The Ubuntu repositories carry PostgreSQL, but they lag the upstream release by months. For production hosts where you want the current major version, point patches, and a clean upgrade path, install from the PGDG (PostgreSQL Global Development Group) repository. This article walks installation of PostgreSQL 16 on Ubuntu 22.04/24.04 from PGDG, verification of the cluster, and the day-zero setup that prepares the host for backups and monitoring.
How to verify
Before you install, confirm the OS, available disk, and that no prior Postgres install is squatting:
lsb_release -a
df -h /var/lib /var/log
dpkg -l | grep -E 'postgresql|postgis'
sudo pg_lsclusters 2>/dev/null
ss -tlnp | grep 5432
If pg_lsclusters returns rows or /var/lib/postgresql/ contains a main directory, there is an existing cluster — do not install over it.
What’s happening
The PGDG project maintains apt repositories that track every supported Postgres major version. The repository ships several packages: postgresql-16 (the server), postgresql-client-16 (the psql client), postgresql-contrib-16 (extensions like pg_stat_statements, pgcrypto), and dev headers. The Ubuntu wrapper packages (postgresql-common) install a per-version cluster manager — you can run 14, 15, and 16 side-by-side on the same host with different ports if needed.
The default cluster is named main and lives under /etc/postgresql/16/main/ (configuration) and /var/lib/postgresql/16/main/ (data). The systemd unit postgresql@16-main.service controls it; the umbrella postgresql.service starts all clusters configured to auto-start. The default port is 5432; subsequent clusters get 5433, 5434, and so on.
PostgreSQL on Ubuntu uses peer authentication for local Unix-socket connections — meaning the OS user postgres connects without a password, and any user matching a database role can connect as that role. That is the right default for admin work; application traffic uses TCP with password (or scram-sha-256) auth.
The procedure
-
Add the PGDG repository.
sudo install -d /usr/share/postgresql-common/pgdg sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc \ --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] \ https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" \ > /etc/apt/sources.list.d/pgdg.list' sudo apt update -
Install PostgreSQL 16 and contrib.
sudo apt install -y postgresql-16 postgresql-contrib-16 -
Confirm the cluster is up.
sudo pg_lsclusters # Expected: # Ver Cluster Port Status Owner Data directory Log file # 16 main 5432 online postgres /var/lib/postgresql/16/main /var/log/postgresql/postgresql-16-main.log sudo systemctl status postgresql@16-main --no-pager -
Connect as the postgres OS user.
sudo -u postgres psql -c "SELECT version();" -
Set a password for the postgres role. Required for any future remote connections as postgres (which you should rarely do, but you need it for emergencies).
sudo -u postgres psql \password postgres # Enter and confirm a strong password \q -
Decide on the listen address. Default is
localhost. To accept connections from a private subnet, edit/etc/postgresql/16/main/postgresql.conf:listen_addresses = 'localhost,10.0.0.5' port = 5432 -
Configure host-based authentication. Edit
/etc/postgresql/16/main/pg_hba.confand add (for example) a CIDR-scoped TLS-required line:# TYPE DATABASE USER ADDRESS METHOD hostssl all all 10.0.0.0/24 scram-sha-256See postgres-roles-pghba for the full pg_hba.conf reference.
-
Reload. Changes to
postgresql.conf(most of them) and any change topg_hba.confrequire:sudo systemctl reload postgresql@16-mainChanges to
listen_addresses,port, orshared_buffersrequire restart, not reload. -
Tune
shared_buffers. The single most impactful setting. Default is 128 MiB; for a dedicated host with 16 GB RAM, set 4 GB:# /etc/postgresql/16/main/postgresql.conf shared_buffers = 4GB effective_cache_size = 12GB # OS page cache estimate work_mem = 16MB maintenance_work_mem = 1GB wal_buffers = 16MB checkpoint_timeout = 15min max_wal_size = 4GBRestart:
sudo systemctl restart postgresql@16-main. -
Open the firewall if remote app hosts will connect:
sudo ufw allow from 10.0.0.0/24 to any port 5432 proto tcp sudo ufw status verbose
Common pitfalls
- The Ubuntu cluster wrapper expects all configuration in
/etc/postgresql/<version>/<cluster>/. Editingpostgresql.confdirectly under/var/lib/postgresql/<version>/main/works but the wrapper overwrites changes during package upgrades. peerauthentication on Ubuntu means only OS userpostgrescan connect as DB rolepostgresover the socket. Trying to runpsql -U postgresas your own user gets “peer authentication failed” — usesudo -u postgres psqlor connect via TCP with a password.- The
postgressuperuser role has no password by default — local socket connections do not need one. Set a password before exposing TCP. pg_hba.confis read top-to-bottom and the first matching line wins. A permissivehost all all 0.0.0.0/0 trustline at the top makes everything below it meaningless. Always order from most-specific to least-specific.- Reading the log via
journalctl -u postgresqlshows only the systemd-emitted lines; the actual server log is at/var/log/postgresql/postgresql-16-main.log(configured bylog_destinationandlogging_collector).
In the engagements we run, the first hours of a new Postgres host include enabling pg_stat_statements for query telemetry, scheduling the first base backup (pgBackRest or barman), and wiring postgres_exporter into Prometheus. We carry that under Managed Operations so the install-day checklist is operational policy, not tribal knowledge.