Home

Installing PostgreSQL 12 on Ubuntu 20.04 LTS

This is a quick walkthrough of installing PostgreSQL on Ubuntu and getting the basic connection settings in place. The version specifically is 12 — swap the version suffix at install time for whatever you actually need.

1. Add the PostgreSQL apt repo

Add the official PGDG repo so you can pull a recent PostgreSQL instead of whatever's in Ubuntu's default repos.

bash
cd /etc/apt/sources.list.d/touch pgdg.listnano pgdg.list

Add this line:

ini
deb http://apt.postgresql.org/pub/repos/apt/ focal-pgdg main

Sign the new repo:

bash
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Note: on newer Ubuntu releases, apt-key add is deprecated. Use a keyring file under /etc/apt/keyrings/ and reference it with signed-by= in the sources file instead.

Refresh apt and install PostgreSQL 12 (change 12 to whatever major version you want):

bash
sudo apt updatesudo apt install postgresql-12

apt will tell you the disk footprint — confirm with Y.

2. Configure pg_hba.conf

PostgreSQL is installed but only listens on localhost out of the box. To let other hosts connect, edit pg_hba.conf:

bash
cd /etc/postgresql/12/main/nano pg_hba.conf

Add a line for the subnet you want to allow. I'm using 192.168.10.0/24 here — substitute your own.

bash
host all all 192.168.10.0/24 trust

For replication clients, add:

bash
host replication all 192.168.10.0/24 trust

trust skips authentication for that subnet, which is fine on a private network for a quick test but a terrible idea on anything publicly reachable. Use md5 or scram-sha-256 for real environments.

3. Configure postgresql.conf

Last bit: tell PostgreSQL to bind to all interfaces, not just localhost.

bash
nano /etc/postgresql/12/main/postgresql.conf

Find listen_addresses, uncomment it, and set it to *:

bash
listen_addresses = '*'

Restart Postgres so the new config takes effect:

bash
sudo systemctl restart postgresql

4. Default user

The default superuser is postgres. From the host, you can drop into the SQL shell with:

bash
sudo -u postgres psql

Set a real password for the postgres user before exposing this anywhere:

sql
ALTER USER postgres WITH PASSWORD 'something-strong';

That's it — PostgreSQL 12 is up and accepting connections.