Verified guide
This guide was verified by creating a cloud server and running every command in it, from start to finish, exactly as written.
- OS
- Ubuntu 24.04.5 LTS · x86_64
- Installed version
- PostgreSQL 16.15 (16.15-0ubuntu0.24.04.1)
- Test server
- DigitalOcean · 1 vCPU / 1 GB RAM / 25 GB SSD (s-1vcpu-1gb, sgp1)
- Last checked
- 2026-10-09
This guide contains affiliate links. If you sign up through them, Codesand may earn a commission. This doesn't affect the content or our assessments.
Install PostgreSQL 16 on an Ubuntu 24.04 server, create a user and a database for remote access, and then configure remote access and the firewall so that only your PC can connect.
1. Requirements
- One Ubuntu 24.04 LTS server (1 GB of memory or more recommended)
- SSH access with an account that has
sudoprivileges - The public IP address of the PC you will connect from (e.g.
203.0.113.10)
If you don't have a server, create the smallest DigitalOcean Droplet (1 vCPU · 1 GB). This Droplet is enough to install PostgreSQL 16. Every step in this guide was verified on a server created as a Droplet (1 vCPU · 1 GB memory · 25 GB SSD · Ubuntu 24.04 · Singapore).
2. Install PostgreSQL
The default Ubuntu 24.04 repositories include PostgreSQL 16. Update the package list, then install it.
sudo apt update
sudo apt install -y postgresql postgresql-contribWhen the installation finishes, check the installed version.
psql --versionpsql (PostgreSQL) 16.15 (Ubuntu 16.15-0ubuntu0.24.04.1)3. Check the service status
When the installation finishes, the default cluster 16/main is created automatically and runs on port 5432.
pg_lsclustersVer Cluster Port Status Owner Data directory Log file
16 main 5432 online postgres /var/lib/postgresql/16/main /var/log/postgresql/postgresql-16-main.logsudo systemctl status postgresql@16-main --no-pager● postgresql@16-main.service - PostgreSQL Cluster 16-main
Loaded: loaded (/usr/lib/systemd/system/postgresql@.service; enabled-runtime; preset: enabled)
Active: active (running) since Fri 2026-10-09 10:42:19 UTC; 20s agoNow check that you can connect to PostgreSQL. Right after installation, you can connect only as the Linux account postgres.
sudo -u postgres psql -c "SELECT version();" version
------------------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 16.15 (Ubuntu 16.15-0ubuntu0.24.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0, 64-bit
(1 row)4. Create a user and a database
It is safer for applications to connect as a dedicated user instead of the admin account postgres. Create the user appuser and the database appdb owned by this user. Be sure to replace the password with a value of your own.
sudo -u postgres psql -c "CREATE ROLE appuser WITH LOGIN PASSWORD 'change-this-password';"
sudo -u postgres psql -c "CREATE DATABASE appdb OWNER appuser;"CREATE ROLE
CREATE DATABASECheck that the user was created.
sudo -u postgres psql -c "\du" List of roles
Role name | Attributes
-----------+------------------------------------------------------------
appuser |
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS5. Check the connection on the server
To connect with a password, you must add -h localhost. This connects over TCP and checks the password.
psql -h localhost -U appuser -d appdb -c "\conninfo"Password for user appuser:
You are connected to database "appdb" as user "appuser" on host "localhost" (address "::1") at port "5432".
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)6. Allow remote connections
The default settings allow only localhost connections. To allow remote connections, you need to edit two files.
First, change listen_addresses in postgresql.conf so that the server also accepts connections on external addresses.
sudo sed -i "s/^#listen_addresses = 'localhost'/listen_addresses = '*'/" /etc/postgresql/16/main/postgresql.conf
grep -n '^listen_addresses' /etc/postgresql/16/main/postgresql.conf60:listen_addresses = '*' # what IP address(es) to listen on;Next, add one line to pg_hba.conf with the IP, user and database to allow. Replace 203.0.113.10 with the public IP of the PC you will connect from.
echo 'host appdb appuser 203.0.113.10/32 scram-sha-256' | sudo tee -a /etc/postgresql/16/main/pg_hba.conf
sudo systemctl restart postgresqlCheck that port 5432 is now open on external addresses as well. If you see 0.0.0.0:5432, it is working.
sudo ss -tlnp | grep 5432LISTEN 0 200 0.0.0.0:5432 0.0.0.0:* users:(("postgres",pid=4188,fd=6))
LISTEN 0 200 [::]:5432 [::]:* users:(("postgres",pid=4188,fd=7))7. Configure the firewall (ufw)
Separately from the PostgreSQL settings, allow only your PC's IP to reach port 5432 in the firewall as well. Always allow SSH before you enable the firewall. If you enable the firewall without allowing SSH, your connection to the server is cut off.
sudo ufw allow OpenSSH
sudo ufw allow from 203.0.113.10 to any port 5432 proto tcp
sudo ufw --force enable
sudo ufw statusStatus: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
5432/tcp ALLOW 203.0.113.10
OpenSSH (v6) ALLOW Anywhere (v6)8. Check the connection from your PC
If psql is installed on your PC, connect to the server's public IP. Replace 203.0.113.20 with the server's IP.
psql -h 203.0.113.20 -U appuser -d appdb -c "\conninfo"You are connected to database "appdb" as user "appuser" on host "203.0.113.20" at port "5432".
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)If psql isn't installed on your PC, you can run the PostgreSQL client with Docker and connect. Because the --rm option is added, the container is deleted automatically when you end the session.
docker run --rm -it postgres:16-alpine psql -h 203.0.113.20 -U appuser -d appdbSSL connection (protocol: TLSv1.3 …) in the output means that the connection is encrypted. Ubuntu's PostgreSQL package turns on SSL by default with a self-signed certificate at install time.
9. Troubleshooting
E: Could not get lock /var/lib/apt/lists/lock. It is held by process … (apt-get)
If the server was created only recently, cloud-init and automatic updates are using apt, which causes this error. Wait until they finish with cloud-init status --wait, then run the command again. Do not delete the lock file yourself, as that can damage the package database.
FATAL: Peer authentication failed for user "appuser"
This error occurs because connecting without -h uses peer authentication. Add -h localhost, as in psql -h localhost -U appuser -d appdb, to connect with a password.
FATAL: no pg_hba.conf entry for host "203.0.113.10", user "postgres", database "appdb"
This means that the combination of IP, user and database you connected with is not in pg_hba.conf. This guide allows only appuser to connect to appdb, so this error is expected when you connect remotely as postgres. To allow other users or databases, add a line to pg_hba.conf and run sudo systemctl reload postgresql.
Remote connections time out with no response
The firewall is blocking the connection. Check that your PC's IP is listed in sudo ufw status and that the public IP of your home or office hasn't changed. If you also use your cloud provider's firewall (DigitalOcean Cloud Firewall, AWS security groups, etc.), allow port 5432 there as well.
10. Uninstall
To delete PostgreSQL and all of its data, run the commands below. Because the database files are deleted as well, back up any data you need with pg_dump first.
sudo apt purge -y 'postgresql*'
sudo rm -rf /var/lib/postgresql /etc/postgresql
sudo ufw delete allow from 203.0.113.10 to any port 5432 proto tcpTry it yourself
No server yet? You can spin up an Ubuntu server in a few minutes with one of these providers. We verified this guide on DigitalOcean.
Was this guide helpful? Let us know if anything is wrong or out of date.
Request a correction