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.
cd /etc/apt/sources.list.d/touch pgdg.listnano pgdg.listAdd this line:
deb http://apt.postgresql.org/pub/repos/apt/ focal-pgdg mainSign the new repo:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -Note: on newer Ubuntu releases,
apt-key addis deprecated. Use a keyring file under/etc/apt/keyrings/and reference it withsigned-by=in the sources file instead.
Refresh apt and install PostgreSQL 12 (change 12 to whatever major version you want):
sudo apt updatesudo apt install postgresql-12apt 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:
cd /etc/postgresql/12/main/nano pg_hba.confAdd a line for the subnet you want to allow. I'm using 192.168.10.0/24 here — substitute your own.
host all all 192.168.10.0/24 trustFor replication clients, add:
host replication all 192.168.10.0/24 trust
trustskips authentication for that subnet, which is fine on a private network for a quick test but a terrible idea on anything publicly reachable. Usemd5orscram-sha-256for real environments.
3. Configure postgresql.conf
Last bit: tell PostgreSQL to bind to all interfaces, not just localhost.
nano /etc/postgresql/12/main/postgresql.confFind listen_addresses, uncomment it, and set it to *:
listen_addresses = '*'Restart Postgres so the new config takes effect:
sudo systemctl restart postgresql4. Default user
The default superuser is postgres. From the host, you can drop into the SQL shell with:
sudo -u postgres psqlSet a real password for the postgres user before exposing this anywhere:
ALTER USER postgres WITH PASSWORD 'something-strong';That's it — PostgreSQL 12 is up and accepting connections.