Ubuntu 24.04 ships PostgreSQL in its own repositories (PostgreSQL 16 at the time of writing). The PostgreSQL Global Development Group also provides an official Apt repository (PGDG) with newer major versions (e.g. PostgreSQL 18).
In this post I’ll run the same commands twice:
- Once with Ubuntu’s default repository only.
- Once after enabling the official PostgreSQL Apt repository.
That way you can see exactly how:
apt-cache policy postgresql
sudo apt install postgresql
behave differently before and after PGDG.
Tested on Ubuntu Server 24.04.3 LTS (noble) with a sudo-enabled user.
- Install PostgreSQL from the default Ubuntu repository
This is the “out-of-the-box” behavior on a fresh Ubuntu 24.04.
Check what postgresql will install
apt-cache policy postgresql
Example output:
postgresql:
Installed: (none)
Candidate: 16+257build1.1
Version table:
16+257build1.1 500
500 http://tw.archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages
16+257build1 500
500 http://tw.archive.ubuntu.com/ubuntu noble/main amd64 Packages
Interpretation:
- Only Ubuntu’s own repo is listed.
- The
postgresqlmeta-package will install PostgreSQL 16 from Ubuntu.
Install PostgreSQL
sudo apt install postgresql
This pulls in:
postgresql-16postgresql-client-16postgresql-common- dependencies like
libpq5,libllvm17, etc.
Check the server version
sudo -u postgres psql -c "SELECT version();"
Example:
PostgreSQL 16.11 (Ubuntu 16.11-0ubuntu0.24.04.1) on x86_64-pc-linux-gnu, ...
At this point you have a working PostgreSQL 16 from the Ubuntu repo.
Remove this Ubuntu PostgreSQL installation
To keep the demo clean, remove it before moving to PGDG:
sudo apt purge 'postgresql*'
sudo apt autoremove -y
This removes:
postgresql,postgresql-16,postgresql-client-16,postgresql-common,postgresql-client-common,- and any unused dependencies.
(If you actually want to keep the Ubuntu install, you can skip this purge in your real environment. Here it’s just for showing a clear before/after.)
- Enable the PostgreSQL Apt repository and install again
Now we enable the official PostgreSQL Apt repo and run the same commands.
Install postgresql-common (for the helper script)
First, reinstall postgresql-common (we purged it above):
sudo apt install -y postgresql-common
This installs the helper script:
/usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
Enable the PostgreSQL Apt repository (PGDG)
Run the script and accept the defaults when it says it will use noble-pgdg:
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
The script will:
- Add the PGDG signing key.
- Add a
pgdgentry under/etc/apt/sources.list.d/fornoble-pgdg. - Run
apt-get updatefor you.
Check what postgresql will install now (Ubuntu + PGDG)
Run the same check as in Step 1:
apt-cache policy postgresql
Example output after enabling PGDG:
postgresql:
Installed: (none)
Candidate: 18+287.pgdg24.04+1
Version table:
18+287.pgdg24.04+1 500
500 https://apt.postgresql.org/pub/repos/apt noble-pgdg/main amd64 Packages
16+257build1.1 500
500 http://tw.archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages
16+257build1 500
500 http://tw.archive.ubuntu.com/ubuntu noble/main amd64 Packages
Difference vs Step 1:
- There are now two sources for
postgresql:- Ubuntu archive (PostgreSQL 16).
- PostgreSQL Apt repo (PostgreSQL 18).
- The Candidate is now
18+...from apt.postgresql.org, not16+...from Ubuntu.
Install PostgreSQL again (this time from PGDG)
Run the same command as in Step 1:
sudo apt install postgresql
This time apt will:
- Install
postgresql-18,postgresql-client-18, - Upgrade
postgresql-common,postgresql-client-common,libpq5to PGDG versions, - Create a new cluster
18/main.
Check the server version again
sudo -u postgres psql -c "SELECT version();"
Example:
PostgreSQL 18.1 (Ubuntu 18.1-1.pgdg24.04+2) on x86_64-pc-linux-gnu, ...
(Optional) Create an application user and database
With the PGDG version installed, you can create your application DB as usual.
Connect as postgres:
sudo -u postgres psql
In psql:
CREATE ROLE app_user WITH LOGIN PASSWORD 'change_me_now';
CREATE DATABASE app_db OWNER app_user;
GRANT ALL PRIVILEGES ON DATABASE app_db TO app_user;
\q
Test:
psql -h localhost -U app_user -d app_db -c "SELECT current_database(), current_user;"
(Optional) Enable password auth and remote access
Edit postgresql.conf:
sudo vi /etc/postgresql/18/main/postgresql.conf
Set:
listen_addresses = '*'
Edit pg_hba.conf:
sudo vi /etc/postgresql/18/main/pg_hba.conf
Example:
# IPv4 connections from your LAN
host all all 192.168.0.0/24 scram-sha-256
Restart:
sudo systemctl restart postgresql
If using UFW:
sudo ufw allow 5432/tcp
Then connect from another machine:
psql -h <server-ip> -U app_user -d app_db
Did this guide save you time?
Support this site