How to Self-Host PostgreSQL on a VPS
Install PostgreSQL 18 on Ubuntu 24.04 from the project's apt repository, create an app role, allow remote access safely, back up with pg_dump.
PostgreSQL is a relational database with transactions, JSONB, full-text search and a large set of extensions. This guide installs PostgreSQL 18 by hand on Ubuntu 24.04 from the PostgreSQL project's own apt repository, creates a role and a database for one application, and opens remote access only to the address that needs it.
Prefer one click? Deploy PostgreSQL from the VeloxaHost marketplace and skip the manual steps; this guide is for when you want to run every command yourself.
What you need
- A VeloxaHost server with the Ubuntu 24.04 LTS image. You get root, over SSH or in the browser terminal on the server's page.
- A decision on who connects. An application on the same server needs nothing opened. You on your own computer can use an SSH tunnel. An application on another server needs a port of your block on the shared IPv4 address forwarded to 5432, or a dedicated IPv4 address (a paid add-on; the console shows the current price).
- No domain. Clients connect by address and port.
PostgreSQL's docs set no hardware minimum; memory and disk follow your data. A Veloxa 1 (1 vCPU, 2 GB RAM, 50 GB disk) at $8 a month, billed at $0.02 an hour and capped at the monthly price, suits a small application database. For a busier production database, a Veloxa 2 (2 vCPU, 4 GB RAM, 80 GB disk) at $15 a month, billed at $0.03 an hour and capped at the monthly price, gives PostgreSQL more memory to cache data.
Install PostgreSQL
Ubuntu 24.04 includes PostgreSQL 16 (
apt install postgresql). For the current major version, add the PostgreSQL Apt repository (PGDG) with its setup script, as the download page shows. The script adds the signing key and the repository for your Ubuntu release, and asks you to confirm.sudo apt install -y postgresql-common sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.shInstall PostgreSQL 18.
sudo apt update sudo apt install postgresql-18The package name pins the major version. The repository's
postgresqlmeta-package moves to each new major release, whilepostgresql-18stays on 18 and takes minor releases (18.6 when this was checked). Version 18 is supported until November 14, 2030.The package creates a cluster called
mainand starts it. Data lives in/var/lib/postgresql/18/mainand configuration in/etc/postgresql/18/main. Check that it answers (our command):sudo -u postgres psql -c "SELECT version();"
First login
The postgres operating system user can open psql as the database superuser over the local socket, with no password:
sudo -u postgres psql
Do not use the superuser for applications. Create a role with a password and a database it owns. createuser -P asks for the password twice; createdb -O sets the owner.
sudo -u postgres createuser -P app
sudo -u postgres createdb -O app appdb
Test the new role over TCP on the server (our command). Local socket logins use peer authentication, which only matches an operating system user of the same name, so pass -h localhost:
psql -h localhost -U app appdb
From your own computer. Open a tunnel through the first port of your block, which forwards to SSH. Port 15432 on your side avoids clashing with a local PostgreSQL.
ssh -p 20000 -L 15432:127.0.0.1:5432 root@203.0.113.10
psql "host=localhost port=15432 dbname=appdb user=app"
From an application on another server. Three changes, then a restart:
In
/etc/postgresql/18/main/postgresql.conf, setlisten_addresses = '*'. The default,localhost, only accepts loopback connections, and the setting is read at server start.In
/etc/postgresql/18/main/pg_hba.conf, allow that one role, database and address, over SSL only. Replace the address with your application server's public IPv4.hostssl appdb app 198.51.100.20/32 scram-sha-256On the server's page, under Ports, forward a port of your block to 5432.
sudo systemctl restart postgresql
sudo -u postgres psql -c "SHOW ssl;"
hostssl lines only match SSL connections. Ubuntu's packages normally turn SSL on with a self-signed certificate; if SHOW ssl says off, set up a certificate as the SSL chapter of the docs describes. Clients then connect to the shared address and the forwarded port with sslmode=require.
Secure it
Firewall. VeloxaHost firewalls are network-level and stateful. Until you attach one, all traffic is allowed (except outbound TCP 25). Go to Firewalls, New Firewall, Add Rule: allow TCP 22 only from YOUR.IP/32, and TCP 5432 only from your application server's address, never from 0.0.0.0/0. Attach it on the server's page. Rules match the port on the server, 5432, not the public port of your block.
Two locks, not one. The firewall decides who can reach the port; pg_hba.conf decides who can log in, to which database, and how. Keep both narrow. Never add trust lines for remote addresses, and keep the superuser to local socket logins.
Passwords. scram-sha-256 is the default password method, so roles created as above store salted hashes. Give each application its own role and database rather than sharing one login.
Changes to access rules. After editing pg_hba.conf, reload instead of restarting (our command): sudo systemctl reload postgresql.
Back it up
pg_dump writes a consistent copy of one database while it is in use. The custom format (-Fc) is compressed and restores with pg_restore, which can also pick out single tables. The shell running as root writes the file, and pg_restore reads it on standard input, so the postgres user needs no access to /root.
sudo -u postgres pg_dump -Fc appdb > /root/appdb.dump
sudo -u postgres pg_restore -d appdb < /root/appdb.dump
pg_dump skips roles and other cluster-wide objects. pg_dumpall covers everything:
sudo -u postgres pg_dumpall > /root/all.sql
A daily dump with cron (our schedule; cron needs % escaped):
mkdir -p /root/pg-backups
crontab -e
# add this line:
0 3 * * * sudo -u postgres pg_dump -Fc appdb > /root/pg-backups/appdb-$(date +\%F).dump
On VeloxaHost, snapshots are instant and free (5 on Starter, 30 on Pay as you go): server page, Snapshots, name, Create. Restoring overwrites the current state. A snapshot taken while PostgreSQL runs is like a power cut: on start, PostgreSQL replays its write-ahead log. Automatic snapshots run every hour, 6 hours, 12 hours, daily or weekly, kept for 1, 3, 7, 14 or 30 days. The Backups page creates, downloads and restores server backups (as a new server), on a schedule of every 6 hours, 12 hours, daily or weekly, keeping 1 to 10. Keep dumps as well: they restore a single database and move between major versions.
Update it
Minor releases come through apt. Take a snapshot first; the upgrade restarts the server process.
sudo apt update
sudo apt upgrade
A major version (18 to 19) changes the on-disk format, so the data must be moved. The PGDG repository installs majors side by side: installing postgresql-19 creates a second cluster on port 5433. The upgrade docs show a dump from the old cluster piped straight into the new one, run as postgres:
sudo -u postgres bash
pg_dumpall -p 5432 | psql -d postgres -p 5433
Then stop the old cluster, swap the port values in the two postgresql.conf files, and start the new one. PostgreSQL 19 was at release candidate stage when this was checked; wait for the final release.
Troubleshooting
"Connection refused" from another server. Check listen_addresses and that you restarted, the port forward on the server's page, and the firewall rule for your application's address.
"no pg_hba.conf entry for host". The client reached PostgreSQL but no line matches its address, user, database and SSL use. Fix the line and reload. Add sslmode=require on the client for hostssl lines.
"Peer authentication failed for user app". You connected over the local socket as a different operating system user. Add -h localhost to use a password over TCP.
Slow queries after a resize. PostgreSQL does not retune itself when memory changes. Raise shared_buffers and effective_cache_size in postgresql.conf; shared_buffers needs a restart.
FAQ
Should I install PostgreSQL 16 from Ubuntu or 18 from the PostgreSQL repository?
Both are supported. Ubuntu 24.04 keeps PostgreSQL 16 for the life of the release, and version 16 is supported until November 9, 2028. The PostgreSQL Apt repository gives you 18, supported until November 14, 2030, and later majors when you choose to move.
Is it safe to open port 5432 to the internet?
Only to the addresses that need it. Allow your application server's address in the VeloxaHost firewall and in pg_hba.conf, require SSL with hostssl, and use an SSH tunnel for your own access.
Are VeloxaHost snapshots enough to back up the database?
They are a good first layer: restoring one is like recovering from a power cut, and PostgreSQL replays its log on start. Keep pg_dump files too, because they restore a single database or table and work across major versions.
What license is PostgreSQL under?
The PostgreSQL License, a short permissive license similar to BSD and MIT. You can use, modify and distribute it, including in commercial products, without fees.
Which VeloxaHost size fits PostgreSQL?
A Veloxa 1 (1 vCPU, 2 GB RAM, 50 GB disk) at $8 a month, billed at $0.02 an hour and capped at the monthly price, suits a small application database. A Veloxa 2 (2 vCPU, 4 GB RAM, 80 GB disk) at $15 a month, billed at $0.03 an hour and capped at the monthly price, gives a busier database more memory for its cache.
Commands and settings checked on 2026-10-19 against the official documentation: www.postgresql.org, wiki.postgresql.org. Software changes; if a step no longer matches, the vendor's docs win.
Ready to deploy?
Start a free trial, or compare plans to match your team's scale.