Skip to content

How to Install PostgreSQL and psql on Ubuntu 26.04 and 24.04

4 min read

Applies to: Ubuntu 26.04 LTS (PostgreSQL 18.6) and Ubuntu 24.04 LTS (PostgreSQL 16.15, or 18 from the PostgreSQL apt repository)

To install PostgreSQL on Ubuntu, run sudo apt update && sudo apt install postgresql. That gives you the server and the psql client: PostgreSQL 18 on Ubuntu 26.04 and PostgreSQL 16 on Ubuntu 24.04. If you only need psql to connect to a database somewhere else, install postgresql-client instead.

1. Install the server and psql

sudo apt update
sudo apt install postgresql
psql --version

On Ubuntu 26.04 we got:

psql (PostgreSQL) 18.6 (Ubuntu 18.6-0ubuntu0.26.04.1)

The package creates a first database cluster called main and, on a normal server, starts it. pg_lsclusters shows it:

$ pg_lsclusters
Ver Cluster Port Status Owner    Data directory              Log file
18  main    5432 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log

The configuration files are in /etc/postgresql/18/main/ (16 instead of 18 on Ubuntu 24.04). The real server unit is postgresql@18-main; postgresql.service is only a wrapper, so check the status with:

sudo systemctl status postgresql@18-main

2. Only the psql client

sudo apt install postgresql-client
psql --version

On Ubuntu 24.04 this installed psql (PostgreSQL) 16.15 and no server. According to the PostgreSQL manual, psql works best with servers of the same or an older major version, so use a client at least as new as the server you connect to (see step 3).

3. A newer version on Ubuntu 24.04

Ubuntu keeps the same major version for the life of each release. For PostgreSQL 18 on 24.04, add the PostgreSQL project’s own apt repository with its setup script:

sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
sudo apt install postgresql-18

The script writes /etc/apt/sources.list.d/pgdg.sources. After it ran, our 24.04 test offered postgresql-18 18.6 and postgresql-17 17.11, and the plain postgresql package also pointed to 18. At the time of writing PostgreSQL 18 is the current major version, supported until November 2030.

4. Log in and create a user and database

PostgreSQL creates a Linux user and a database role both called postgres. Local logins use peer authentication: your Linux user name must match the role name. So open psql as that user:

sudo -u postgres psql

Create a role that can log in with a password, and a database it owns:

CREATE ROLE appuser WITH LOGIN PASSWORD 'choose-a-strong-password';
CREATE DATABASE appdb OWNER appuser;
\q

Then connect as the new user over TCP, which uses the password (scram-sha-256) instead of peer:

psql -h localhost -U appuser appdb

Useful commands inside psql: \l lists databases, \du lists roles, \dt lists tables, \conninfo shows how you are connected and \q quits. To change a password later, see change a user password in PostgreSQL.

5. Allow remote connections (only if you need them)

By default the server listens on localhost only. For another server to connect:

  1. In /etc/postgresql/18/main/postgresql.conf, set listen_addresses = '*' (or the server’s own IP address).
  2. In pg_hba.conf in the same folder, allow one database, one user and one source address:
    host    appdb    appuser    203.0.113.10/32    scram-sha-256
  3. Restart: sudo systemctl restart postgresql@18-main.
  4. Open port 5432 in the firewall for that address only, for example with UFW: sudo ufw allow from 203.0.113.10 to any port 5432 proto tcp.

To check the rules the server actually loaded, run sudo -u postgres psql -c "SELECT * FROM pg_hba_file_rules;". Its error column shows any line with a typo.

Errors you may see

  • FATAL: role "root" does not exist: you ran psql as root. Use sudo -u postgres psql, or give a user with -U and -h localhost.
  • FATAL: Peer authentication failed for user "appuser": you connected through the local socket as a Linux user with a different name. Add -h localhost to use the password instead.
  • FATAL: password authentication failed for user "appuser": wrong password, or the role has none set.
  • FATAL: no pg_hba.conf entry for host "172.17.0.2", user "appuser", database "appdb", SSL encryption: the connection reached the server, but no pg_hba.conf line matches that address, user and database.
  • connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory or Connection refused over TCP: the server is not running. Check pg_lsclusters and the log file it lists.

Choosing between databases for an app? See install MySQL on Ubuntu. Running n8n: use PostgreSQL instead of SQLite with n8n. A KVM VPS gives you the root access this needs.

Official documentation: PostgreSQL on Ubuntu, the pg_hba.conf file and versioning policy.

Was this answer helpful?

Still stuck? Open a ticket