PostgreSQL is one of the best-known relational open-source databases. Many web applications, Discord bots, backends built with frameworks like Django, Rails, or Prisma, and even self-hosted tools store their data in PostgreSQL. With your own database on your VPS, you control its size, version, and access, and you don't pay per database instance.
In this guide, we'll show you two ways to set it up: the simple way, as a One-Click App, which runs PostgreSQL 17 with an automatically generated user and password in just a few minutes, and a manual installation with Docker Compose via the console. Then you'll learn how to secure the database port, connect applications, create your own databases and users, and back up your data.
Your own PostgreSQL database is a good choice if:
- Your software requires or recommends PostgreSQL, such as your own backend, a Discord bot, or an application you're developing yourself.
- Several projects need to use a shared database, each with its own user and database.
- An application on another server needs a database, such as a game server plugin or a web app on your web hosting space.
- You want to use a real database while developing instead of SQLite and access it from your PC via an SSH tunnel.
- You want control over the version and backups, instead of paying for a database service per instance.
The app is less suitable if another One-Click App already includes its own database. Variants like “Forgejo with PostgreSQL” or “Gitea with PostgreSQL” start their own database, so you don't need an additional PostgreSQL app. If your software requires MySQL or MariaDB, such as WordPress or FiveM with oxmysql, use the MariaDB One-Click App. See Set Up MariaDB on a Linux Server.
With your VPS's One-Click Apps, you can start a standalone PostgreSQL 17 server. During installation, the app creates a database, a user with a random name, and a long, random password. The data is stored in a persistent volume on your VPS.
- A Linux VPS (Debian or Ubuntu) with One-Click Apps set up. See VPS: One-Click Apps for instructions.
- At least 256 MB RAM and 1 GB of storage for a small database. 1 GB RAM, 2 vCPU cores, and 10 GB of storage are recommended. Your actual requirements depend on the amount of data and the number of queries.
- Port
5432must not already be in use on the VPS. Otherwise, choose a different port during installation.
- Open your VPS in the ZapX dashboard and go to the One-Click Apps tab.
- Search for “PostgreSQL” in the App catalog and click Install app for PostgreSQL.
- Under App name, you can enter a custom name, for example “PostgreSQL Bots” if you run several databases.
- Under App configuration, you can change two values. This isn't required:
| Field | Default | Description |
|---|---|---|
POSTGRES_DB | app | Name of the database created during installation |
POSTGRES_PORT | 5432 | Port used to access the database on your VPS |
- Click Queue installation. The app will be Online after about a minute.
You don't need a custom domain; PostgreSQL has no web interface. The app card shows the database address under Connection, for example 123.123.123.123:5432.
POSTGRES_DB only takes effect on the very first startup, while the volume is still empty. Changing it later won't create a new database. You can create additional databases as described in “Create a Database and User for an Application.”
The app generates a username and password during installation. You'll find them in the app settings (gear icon) under App environment > Automatically generated values. Click the eye icon to reveal a value, or the copy icon to copy it.
| Value | Description |
|---|---|
SERVICE_USER_POSTGRES | Username, a random string of lowercase letters |
SERVICE_PASSWORD_POSTGRES | This user's password, 32 random characters |
This user is a superuser on the PostgreSQL server. It can do everything, including creating additional databases and users. Don't give its credentials to applications; instead, create a separate user for each application.
You'll need the following details for each application that uses your database:
| Setting | Value |
|---|---|
| Host | IP address of your VPS |
| Port | 5432 or the value of POSTGRES_PORT |
| Database | app or the value of POSTGRES_DB |
| Username and password | SERVICE_USER_POSTGRES and SERVICE_PASSWORD_POSTGRES, or the user you create for the application |
Many applications expect these details in a single line called a connection URL:
postgresql://DB_USER:DB_PASSWORD@123.123.123.123:5432/app
If the password contains special characters such as @, :, or /, you must encode them in the URL. For example, encode @ as %40. If your application runs on the same VPS, it can also use the VPS's IP address. Programs running directly on the VPS (not in Docker) can also use 127.0.0.1.
You don't need a separate program for a quick test. Connect to your VPS via SSH and open psql in the app container:
docker ps --filter name=postgresql-
docker exec -it CONTAINER_NAME psql -U DB_USER -d app
The first command shows the container name, which starts with postgresql-. In the container, psql won't prompt you for a password. Use \l to list the databases and \q to exit psql.
The app exposes PostgreSQL on all of your VPS's addresses. Anyone on the internet can reach the port and try passwords, and the connection is not encrypted because the app doesn't configure TLS. The generated password is long and random, but you should still restrict the port to the IP addresses that actually need to connect as soon as you finish installing the app.
The firewall on the VPS itself (ufw) won't help here: ports published by Docker are accessible even when ufw blocks them. Use the Network firewall in the ZapX dashboard instead. It filters traffic before it reaches your VPS:
- Open your VPS in the ZapX dashboard and go to the Network firewall tab. Make sure Firewall active is turned on.
- Click Add rule. Set Direction to
IN, Action toACCEPT, and Service template toPostgreSQL(TCP 5432). If you chose a different port, select Custom, set the protocol totcp, and enter your port as the Destination port. - Under Source, enter the IP address that is allowed to connect, for example the address of your game server or web server, and click Create. For multiple addresses, create one rule per address or group them under IP Sets.
- Add a second rule: Direction
IN, ActionDROP, Service templatePostgreSQL, and leave Source empty. - Check the order under Custom rules: the first matching rule takes precedence, so the
ACCEPTrule must be above theDROPrule. New rules are added to the end of the list; use the arrows to move them.
Connections from apps on the same VPS don't pass through the Network Firewall and will continue to work. For more information about ports and the firewall, see VPS: Open Ports and Manage the Firewall on Linux.
You don't need to open port 5432 for your own access from your PC, for example with a database program. Open an SSH tunnel with ssh -L 5432:127.0.0.1:5432 root@SERVER_IP and connect your program to localhost:5432. This also encrypts the connection.
- Updates: Open the app settings (gear icon) > App update and click Check for updates. If a new version is available, install it by clicking Apply update. Your data will be preserved. The app stays on PostgreSQL 17; it won't automatically switch to a new major version.
- Backups: A VPS backup from the Backups tab includes the volume. You can also back up an individual database with
pg_dump, as described under “Back Up Your Data.” You don't need to stop the app to create a dump. - Uninstall: Under App actions > Uninstall app, you can also delete the data volumes. This permanently removes all databases. Create a dump first.
The rest of this guide describes a manual installation of PostgreSQL with Docker Compose via the console, for example on a server without One-Click Apps. The commands are written for Debian and Ubuntu. Unlike the One-Click App, PostgreSQL will initially listen only on the server itself.
| Minimum | Recommended | |
|---|---|---|
| CPU | 1 vCPU core | 2 vCPU cores |
| RAM | 256 MB | 1 GB |
| Storage | 1 GB free (image is just over 400 MB) | 10 GB, depending on the amount of data |
These values are for small databases using PostgreSQL's default settings. You'll also need Docker with the Compose plugin, as described in Set Up Docker on a Linux Server, and SSH access. See VPS: First Access via SSH.
PostgreSQL doesn't need an open port for this guide because it's bound only to 127.0.0.1. Open only SSH in ufw:
sudo apt install -y ufw
sudo ufw allow OpenSSH
sudo ufw enable
Ports that Docker publishes on all addresses with ports: are accessible from outside even when ufw blocks them. So leave 127.0.0.1: before the port in the Compose file unless you need external access.
Create a folder and create a .env file in it with a random password:
sudo mkdir -p /opt/postgresql
cd /opt/postgresql
echo "POSTGRES_PASSWORD=$(openssl rand -hex 24)" | sudo tee .env > /dev/null
sudo chmod 600 .env
sudo nano compose.yaml
Add the following to compose.yaml:
services:
postgresql:
image: postgres:17-alpine
container_name: postgresql
restart: unless-stopped
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: app
ports:
- "127.0.0.1:5432:5432"
volumes:
- postgresql-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 10s
timeout: 5s
retries: 10
start_period: 20s
volumes:
postgresql-data:
The important lines:
image: postgres:17-alpineis the same version as the One-Click App. You can install minor updates within version 17 withdocker compose pull.POSTGRES_USER,POSTGRES_PASSWORD, andPOSTGRES_DBcreate the superuser and the first database on the first startup. The password comes from.env. As with the One-Click App, these values only take effect while the volume is empty.127.0.0.1:5432:5432makes PostgreSQL accessible only from the server itself. Programs on the server connect to127.0.0.1:5432.- The
postgresql-datavolume contains all databases. It persists when you recreate the container. - The
healthcheckreports the container as ready only after PostgreSQL accepts connections.
Start PostgreSQL and check its status:
sudo docker compose up -d
sudo docker compose ps
After about 20 seconds, the STATUS column should show healthy.
Open psql in the container:
sudo docker exec -it postgresql psql -U admin -d app
Use SELECT version(); to see the running version, and \q to exit psql. To use database programs on your PC, use the SSH tunnel from the tip under “Secure Access.” The password is in .env.
If an application on another server needs access, change the line under ports: to "5432:5432" and restart with sudo docker compose up -d. Then restrict the port to the IP addresses of those servers. On a ZAP-Hosting VPS, do this in the Network firewall, as described under “Secure Access.” Keep in mind that without your own TLS configuration, the connection is unencrypted as it travels over the internet.
Back up each database regularly with pg_dump. The -Fc format is compressed and lets you restore data selectively:
sudo mkdir -p /root/backups
sudo docker exec postgresql pg_dump -U admin -Fc app > /root/backups/app-$(date +%F).dump
For the One-Click App, replace postgresql with the app's container name and admin with the value of SERVICE_USER_POSTGRES. To create daily backups, add the second command as a cron job using crontab -e, for example, with 0 3 * * * at the beginning to run it at 3 a.m. In the cron job, write % as \%.
Restore a dump to an empty database:
sudo docker exec postgresql createdb -U admin app_restore
sudo docker exec -i postgresql pg_restore -U admin -d app_restore < /root/backups/app-2026-01-31.dump
Regularly download the dumps from your server, for example via SFTP. A backup stored only on the same server won't help if the disk fails.
Install updates within version 17 as follows:
cd /opt/postgresql
sudo docker compose pull
sudo docker compose up -d
This isn't enough for a new major version like 18: the data format changes, and PostgreSQL 18 can't use version 17's data directly. In that case, create a dump of all databases with pg_dumpall, start the new version with a new, empty volume, and restore the dump there. Read the new version's release notes first.
These steps work the same way with the One-Click App and the manual installation.
Open psql as described above and create a separate user and database for each application:
CREATE ROLE shop_app LOGIN PASSWORD 'CHANGE_ME';
CREATE DATABASE shop OWNER shop_app;
Your application can then connect to the shop database using the shop_app user. It has full permissions there, but no access to other databases. Replace CHANGE_ME with a long, random password.
Set a new password in psql with ALTER ROLE shop_app PASSWORD 'NEW_PASSWORD';. This also works for the One-Click App's superuser. Note that the App environment will continue to show the old password because automatically generated values are set only during installation. Keep the new password somewhere safe, such as a password manager.
Would you rather use a graphical interface than psql? The pgAdmin One-Click App runs on the same VPS and connects to the database using your VPS's IP address. You can create tables, write queries, and create backups in your browser. See Set Up pgAdmin on a Linux Server for instructions. For information about other database systems like MariaDB, Redis, or MongoDB, see Set Up Databases on a Linux Server.
- No encryption by default: The One-Click App doesn't configure TLS. For access over the internet, use an SSH tunnel or restrict the port to specific IP addresses.
- No web interface: You can manage the database with
psql, a program on your PC, or pgAdmin. - Fixed major version: The app uses PostgreSQL 17. You can only switch to a new major version by dumping and restoring the data.
- No replication: The app starts a single server with no standby. If the VPS goes down, the database won't be accessible.
With PostgreSQL on your server, you have your own SQL database for apps and bots, create databases and users, and back up your data with pg_dump. For a browser-based interface, the guide Set Up pgAdmin on a Linux Server shows you how to manage your databases conveniently.
If you have any other questions or need help, our support team is available every day! 🙂
