Skip to content
🧮 Is Lifetime with ZapX Pass worth it? Work it out for your own servers in 30 seconds. Open the Lifetime calculator →

Set Up PostgreSQL on a Linux Server – Run Your Own SQL Database for Apps and Bots

Run PostgreSQL 17 on your VPS as a One-Click App or with Docker Compose. Secure port 5432, connect apps, and back up your data.

2 December 202611 min read2 Views0 helpful

Introduction

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.

When PostgreSQL on a VPS Makes Sense

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.

The Easy Way: PostgreSQL as a One-Click App

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.

Requirements

  • 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 5432 must not already be in use on the VPS. Otherwise, choose a different port during installation.

Install

  1. Open your VPS in the ZapX dashboard and go to the One-Click Apps tab.
  2. Search for “PostgreSQL” in the App catalog and click Install app for PostgreSQL.
  3. Under App name, you can enter a custom name, for example “PostgreSQL Bots” if you run several databases.
  4. Under App configuration, you can change two values. This isn't required:
FieldDefaultDescription
POSTGRES_DBappName of the database created during installation
POSTGRES_PORT5432Port used to access the database on your VPS
  1. 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.

Info: It's best to set the port and database name right away

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.”

Find Your Login Details

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.

ValueDescription
SERVICE_USER_POSTGRESUsername, a random string of lowercase letters
SERVICE_PASSWORD_POSTGRESThis 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.

Connect to the Database

You'll need the following details for each application that uses your database:

SettingValue
HostIP address of your VPS
Port5432 or the value of POSTGRES_PORT
Databaseapp or the value of POSTGRES_DB
Username and passwordSERVICE_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:

Code
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:

Code
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.

Secure Access

Warning: Port 5432 is accessible from the internet

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:

  1. Open your VPS in the ZapX dashboard and go to the Network firewall tab. Make sure Firewall active is turned on.
  2. Click Add rule. Set Direction to IN, Action to ACCEPT, and Service template to PostgreSQL (TCP 5432). If you chose a different port, select Custom, set the protocol to tcp, and enter your port as the Destination port.
  3. 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.
  4. Add a second rule: Direction IN, Action DROP, Service template PostgreSQL, and leave Source empty.
  5. Check the order under Custom rules: the first matching rule takes precedence, so the ACCEPT rule must be above the DROP rule. 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.

Tip: Use an SSH tunnel to connect from your PC

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 and backups

  • 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.

Manual Installation

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.

Requirements

MinimumRecommended
CPU1 vCPU core2 vCPU cores
RAM256 MB1 GB
Storage1 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.

Prepare the Firewall

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:

Code
sudo apt install -y ufw
sudo ufw allow OpenSSH
sudo ufw enable
Warning: Docker bypasses ufw

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.

Install with Docker Compose

Create a folder and create a .env file in it with a random password:

Code
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:

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-alpine is the same version as the One-Click App. You can install minor updates within version 17 with docker compose pull.
  • POSTGRES_USER, POSTGRES_PASSWORD, and POSTGRES_DB create 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:5432 makes PostgreSQL accessible only from the server itself. Programs on the server connect to 127.0.0.1:5432.
  • The postgresql-data volume contains all databases. It persists when you recreate the container.
  • The healthcheck reports the container as ready only after PostgreSQL accepts connections.

Start PostgreSQL and check its status:

Code
sudo docker compose up -d
sudo docker compose ps

After about 20 seconds, the STATUS column should show healthy.

First Connection with psql

Open psql in the container:

Code
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.

Access from Other Servers

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 Your Data

Back up each database regularly with pg_dump. The -Fc format is compressed and lets you restore data selectively:

Code
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:

Code
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.

Update PostgreSQL

Install updates within version 17 as follows:

Code
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.

Manage Databases and Users

These steps work the same way with the One-Click App and the manual installation.

Create a Database and User for an Application

Open psql as described above and create a separate user and database for each application:

sql
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.

Change the 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.

Manage PostgreSQL in Your Browser with pgAdmin

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.

Limitations

  • 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.

Conclusion

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! 🙂

Free tools

Related tools for servers and developers

View all

FAQ

Frequently asked questions

The One-Click App is faster to set up: PostgreSQL 17 is running in about a minute, the username and password are generated automatically, and you can install updates with a click under App update. Manual installation with Docker Compose is a good choice if your server doesn't use One-Click Apps or you want the database accessible only from the server itself, since you can bind the port to 127.0.0.1.
In the app settings (gear icon), go to App environment > Automatically generated values. SERVICE_USER_POSTGRES is the username, and SERVICE_PASSWORD_POSTGRES is the password. Click the eye icon to reveal the value. If you later change the password in psql, the new password applies, but App environment will still show the old value.
Yes. The One-Click App publishes port 5432 on all of your VPS's addresses, and the connection isn't encrypted unless you configure this yourself. Restrict the port to the IP addresses that need access in the Network Firewall tab, and connect from your PC via an SSH tunnel. The ufw firewall on the VPS won't help because Docker bypasses it.

Feedback

Was this article helpful?

Your feedback helps us prioritize and improve content.

0 of 0 people found this article helpful.

Personal support

Need more help than the guide provides?

Our ZAP-Hosting support team knows your product — briefly describe your issue and we'll help you personally.

Zappy knows this guide and answers you right in the chat.