| Tested on | Ubuntu 22.04 LTS — package install, cluster verification, and local psql workflow in Podman (localhost/glc-ubuntu:22.04). Remote-access, systemctl, and UFW steps follow Ubuntu server behavior on a physical or virtual host. |
|---|---|
| Package | postgresql 14+238 / PostgreSQL 14.24postgresql-common 238 |
| Applies to | Ubuntu 22.04 LTS |
| Privilege | sudo or root for packages and config files |
| Scope | Install PostgreSQL 14 on Ubuntu 22.04 from apt (or PGDG when you need PostgreSQL 16, 18, or another major), verify the 14/main cluster, create an application role and database, and configure SCRAM-SHA-256 remote access with UFW. Does not cover major-version upgrades or high availability. |
| Related guides | Install PostgreSQL on Ubuntu 26.04 Install PostgreSQL on Ubuntu 24.04 Install PostgreSQL on Debian Check Ubuntu version |
Ubuntu 22.04 ships PostgreSQL 14 in the default apt repository, so a database server on the same machine is usually one apt install away. Local means applications connect to PostgreSQL on that Ubuntu host; remote means another computer connects over the network.
On Ubuntu, a PostgreSQL cluster is one server instance with its own configuration and data directory — not a multi-server high-availability setup. The installer creates a default cluster named 14/main (version 14, cluster name main) and starts it for you. PostgreSQL listens on TCP port 5432 by default.
Install PostgreSQL from Ubuntu apt on 22.04 when that default major is enough. Add the PostgreSQL Global Development Group (PGDG) apt repo when you need a newer major than Jammy ships. After install, check the cluster with pg_lsclusters, create an application role and database, and open LAN access with scram-sha-256 on TCP instead of legacy MD5.
Before you begin
Unless noted otherwise, run commands on the PostgreSQL server — the Ubuntu machine where you install PostgreSQL.
For remote-access testing, treat the PostgreSQL server as Ubuntu 22.04 at 192.168.1.20 (installation, pg_hba.conf, listen_addresses, systemctl, pg_lsclusters, and UFW) and a remote client on the same LAN at 192.168.1.50 for the final psql -h … test with the client package installed.
Names such as appuser and appdb, and addresses such as 192.168.1.20, are placeholders. Use values that match your application and network.
If applications connect only on the same Ubuntu host, finish after the local connection test and skip Configure PostgreSQL remote access on Ubuntu.
Quick answer
Ubuntu 22.04 includes PostgreSQL 14 in its default repository. Most readers can install it with:
sudo apt update
sudo apt install postgresqlThe installer creates an 14/main cluster and normally starts it automatically. On Ubuntu 22.04 you get PostgreSQL 14 from this command alone — add PGDG when you need PostgreSQL 16, 18, or multiple majors.
Confirm the cluster is online:
pg_lsclustersAdd the official PostgreSQL apt repository when you need a different major version, multiple majors side by side, or packages that Ubuntu does not ship.
Should you still install PostgreSQL 14 on Ubuntu 22.04?
Ubuntu 22.04’s postgresql package still installs PostgreSQL 14 (currently 14.24 in Ubuntu’s repositories). That matches what many Jammy servers run today, but upstream PostgreSQL 14 community support ends on November 12, 2026. Ubuntu’s package-support lifecycle for Jammy is separate from upstream PostgreSQL’s community lifecycle — security fixes for the distro package can continue after upstream EOL, but new deployments should weigh whether PostgreSQL 16 or 18 from PGDG is a better long-term choice before you commit to 14.
If you maintain an existing app pinned to Ubuntu’s PostgreSQL 14 packages, this guide still applies. For a greenfield database in late 2026, read the PGDG section and consider a newer major before you standardize on 14.
In 14.24, 14 is the PostgreSQL major version and .24 is its current minor maintenance release, delivered through Ubuntu package updates.
Which installation method should you use?
| Method | Best for | Jump to |
|---|---|---|
Ubuntu repository (apt install postgresql) |
Ubuntu 22.04 servers that want the supported distro package (PostgreSQL 14 today) | Method 1 |
| PostgreSQL PGDG apt repository | A different major, multiple majors, or PGDG-only packages | Method 2 |
On Ubuntu 22.04, start with the Ubuntu repository unless you need a newer major from PGDG.
Install PostgreSQL 14 on Ubuntu 22.04
Install PostgreSQL from the Ubuntu repository
Refresh package indexes, then install the postgresql metapackage with the apt command:
sudo apt updateInstall the server metapackage next:
sudo apt install -y postgresqlOn Ubuntu 22.04 this pulls PostgreSQL 14. The postgresql package is a metapackage: it does not contain the server binaries itself; it installs the PostgreSQL 14 server packages for this release. It depends on postgresql-common, which manages clusters. Ubuntu’s PostgreSQL 14 build includes the standard supplied extensions; install extra extension packages only when you need add-ons beyond that set.
Verify the PostgreSQL version
psql is PostgreSQL’s command-line client (not the server). sudo -u postgres runs the next command as the Linux account created for PostgreSQL administration; how that maps to the database postgres role is explained below.
Ask the server which version is running (-c runs one SQL command and exits):
sudo -u postgres psql -c "SELECT version();"Sample output:
version
---------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 14.24 (Ubuntu 14.24-0ubuntu0.22.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0, 64-bit
(1 row)The leading PostgreSQL 14.x string should show major version 14 on Ubuntu 22.04.
Check the PostgreSQL cluster status
On Debian and Ubuntu, postgresql.service is a cluster manager. The database you connect to is a versioned cluster such as 14/main. pg_lsclusters shows the cluster that actually serves connections:
pg_lsclustersSample output:
Ver Cluster Port Status Owner Data directory Log file
14 main 5432 online postgres /var/lib/postgresql/14/main /var/log/postgresql/postgresql-14-main.logVer is the PostgreSQL major version, main is Ubuntu’s default cluster name (not your application database name), and 5432 is the default TCP port. Status should read online. If it shows down, see PostgreSQL cluster shows down before changing configuration.
Ubuntu manages each cluster with a versioned systemd unit; see systemctl for status, restart, and reload. For the 14/main cluster:
sudo systemctl status postgresql@14-main --no-pagerThe Active: active (running) line confirms the 14/main instance is up. You normally do not need systemctl enable after a fresh apt install — packaging enables the cluster unit when it is created.
Optional: Install PostgreSQL from the official PGDG repository
apt install postgresql already gave you PostgreSQL 14, skip this entire section unless you need a newer major from PGDG — then continue with Understand the default Ubuntu PostgreSQL setup after PGDG install.
When you need the PostgreSQL repository
Use the PGDG apt repository when PostgreSQL 14 from Ubuntu apt is not what you need — for example PostgreSQL 16 or 18, or a PGDG-only package stream. Running two PostgreSQL majors on one host is supported, but each cluster needs its own port and should be planned deliberately. Skip this section if apt install postgresql already gives you PostgreSQL 14 and that is enough.
Add the PGDG repository
Install postgresql-common if it is not already present, then run the helper script maintained with the PostgreSQL apt packages:
sudo apt install -y postgresql-commonRun the PGDG setup script shipped with postgresql-common:
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.shThe helper configures the PGDG source and signing key for your Ubuntu release. Depending on the installed postgresql-common version, the generated repository file may use the newer Deb822 .sources format or an older .list layout — you do not need to add the repository manually.
Refresh indexes after the repository is added:
sudo apt updateInstall a specific PostgreSQL version
On a host that already runs Ubuntu’s PostgreSQL 14, pick a different major only when you intend to run multiple clusters:
sudo apt install -y postgresql-16To install another supported major, use the matching package (for example postgresql-17) instead of postgresql-16.
List clusters again — each major appears as its own row:
pg_lsclustersUnderstand the default Ubuntu PostgreSQL setup
Linux postgres user vs PostgreSQL postgres role
Ubuntu creates two different postgres identities:
- Linux account
postgres— owns data files and runs server processes. - PostgreSQL role
postgres— database superuser used for administration.
When you run sudo -u postgres psql, you start psql as the Linux postgres user. Local peer authentication in pg_hba.conf maps that Unix account to the PostgreSQL postgres role without a password. That is the normal way to administer a local server; you do not need to set a password on the postgres superuser just to “secure” a single-machine install.
PostgreSQL configuration and data directories
On Ubuntu 22.04 with PostgreSQL 14, the main paths are:
| Path | Purpose |
|---|---|
/etc/postgresql/14/main/postgresql.conf |
Server settings (listen_addresses, memory, and so on) |
/etc/postgresql/14/main/pg_hba.conf |
Client authentication rules |
/var/lib/postgresql/14/main/ |
Data directory for the 14/main cluster |
/var/log/postgresql/postgresql-14-main.log |
Log file for that cluster |
Create a PostgreSQL user and database
PostgreSQL uses roles for accounts and permissions. A role with login permission behaves like what most people call a database user.
Create a login role
Create an application login role with a password prompt instead of altering the postgres superuser:
sudo -u postgres createuser --pwprompt appuserEnter a unique application password when prompted — not your Ubuntu login password. createuser defines a role your app can use to log in.
Create a database owned by the role
One PostgreSQL server can hold many databases inside a cluster such as 14/main. appdb is one database in that cluster, separate from the built-in postgres maintenance database.
Create a database owned by that role:
sudo -u postgres createdb --owner=appuser appdbThe owner can create and manage objects in the database, so the application does not need the PostgreSQL superuser account.
Test the local connection
Local here means connecting to PostgreSQL on the same machine (127.0.0.1), not a separate “test” database. Socket connections use peer for the postgres Linux user, but applications usually connect over TCP with a password. Test password authentication on the loopback interface:
psql -h 127.0.0.1 -U appuser -d appdb -c "SELECT current_user, current_database();"When psql asks for a password, use the one you set for appuser. A successful run prints your role and database name:
current_user | current_database
--------------+------------------
appuser | appdb
(1 row)That confirms the server is running, the role and database exist, and password-authenticated TCP connections work. On a default current Ubuntu install, host password authentication uses SCRAM-SHA-256 — PostgreSQL’s current password method, preferable to the older MD5 scheme.
Configure PostgreSQL remote access on Ubuntu
All steps in this section run on the PostgreSQL server (192.168.1.20 in the examples) unless stated otherwise.
Example addresses used in this section:
127.0.0.1— the server itself (loopback)192.168.1.20— the server’s LAN address clients connect to192.168.1.50— a remote client on the same network192.168.1.0/24— a sample subnet that contains both.20and.50(use your real client range;/24is only an example size)
Remote clients pass through the host firewall, TCP listen settings, and pg_hba.conf before PostgreSQL checks the password. The flow below is the order to think about when a client cannot connect.
Remote client (192.168.1.50)
↓
UFW: allow 5432 from 192.168.1.0/24
↓
listen_addresses (server LAN IP, not only localhost)
↓
pg_hba.conf: appdb, appuser, client subnet
↓
SCRAM-SHA-256 password check
↓
PostgreSQL 14 cluster — appdb / appuserReplace the example addresses with your LAN. listen_addresses controls which interfaces PostgreSQL binds; pg_hba.conf decides which clients may authenticate. Neither replaces the other.
Configure listen_addresses
If you do not already know the server’s LAN address, list non-loopback IPv4 addresses:
ip -4 addr show scope globalUse the address on the interface your clients reach (for example eth0, ens*, or enp* on cloud VMs), not 127.0.0.1.
Open postgresql.conf in your preferred text editor (for example nano):
sudo nano /etc/postgresql/14/main/postgresql.confSet listen_addresses to that LAN address plus localhost, not only '*' by default:
listen_addresses = 'localhost,192.168.1.20'Use your server’s real LAN IP instead of 192.168.1.20. If the server gets its address from DHCP, reserve a static address or update this setting when the lease changes. Listening on '*' binds all interfaces; that can be valid on private networks, but it still does not authorize clients — pg_hba.conf and the firewall do.
Allow the client in pg_hba.conf
pg_hba.conf is PostgreSQL’s host-based authentication file. It decides which database, role, client address, and authentication method are allowed for each connection type.
Open the file on the PostgreSQL server:
sudo nano /etc/postgresql/14/main/pg_hba.confAdd one line that names the database, role, and client subnet. Use scram-sha-256 (SCRAM-SHA-256), which Ubuntu defaults to for TCP connections and which PostgreSQL recommends instead of MD5:
host appdb appuser 192.168.1.0/24 scram-sha-256Reading the host line left to right:
host— TCP connection (with or without TLS unless you usehostssl)appdb— database name clients may connect toappuser— role name clients may use192.168.1.0/24— client source networkscram-sha-256— password authentication method
Avoid host all all 0.0.0.0/0 rules. They expose every database and role to the entire IPv4 Internet if the host is reachable.
A host rule matches TCP connections whether or not TLS is used. If your environment requires encrypted database sessions only, use hostssl with the same database, role, and subnet fields, and configure PostgreSQL TLS separately — this guide does not cover certificates.
Apply configuration changes
A pg_hba.conf-only change takes effect after systemctl reload, but listen_addresses is read only at server start. Because you changed both files, restart the cluster once:
sudo systemctl restart postgresql@14-mainConfirm PostgreSQL is listening on the addresses you expect. The ss command lists TCP listeners (-lnt); filter for the cluster port:
ss -lnt | grep 5432Allow PostgreSQL through UFW
On the PostgreSQL server, restrict the database port to trusted subnets with UFW. Check whether UFW is active first:
sudo ufw statusAdding a rule while UFW is inactive does not filter traffic until UFW is enabled.
ufw enable on a remote server until you allow SSH (or whatever you use for administration). Locking yourself out is easy.
Allow PostgreSQL only from your trusted subnet:
sudo ufw allow from 192.168.1.0/24 to any port 5432 proto tcpA plain ufw allow 5432/tcp opens the port to any source that can route to the host — tighter subnet rules match how you scoped pg_hba.conf. On a cloud VM, the provider’s security group or network firewall is separate from UFW; both must allow your client network.
Test the remote connection
On the remote client (192.168.1.50 in the examples), install the psql client if it is not already present:
sudo apt install -y postgresql-clientFrom that client host, connect to the server (replace the IP and use the appuser password):
psql -h 192.168.1.20 -U appuser -d appdb -c "SELECT inet_server_addr(), current_user;"inet_server_addr() should show the server address clients reached. If the command hangs or fails, use the troubleshooting table below before widening pg_hba.conf or the firewall.
Keep PostgreSQL updated
Security and minor fixes ship through apt with the rest of the system:
sudo apt update
sudo apt upgradeThat installs available updates from your configured Ubuntu or PGDG sources, including PostgreSQL maintenance releases within your installed major.
After PostgreSQL package updates, run pg_lsclusters if the database does not come back normally. Moving between PostgreSQL majors is a separate migration procedure, not a routine apt upgrade.
Uninstall PostgreSQL
Removing packages is not the same as deleting a cluster. Back up any data you need before you continue.
Delete the PostgreSQL cluster and its data
List clusters first:
pg_lsclusterspg_dropcluster permanently deletes the cluster, including all databases and data files. Do not run it if you only want to remove packages but keep data for recovery.
To permanently remove the 14/main cluster and its data directory, configuration, and logs:
sudo pg_dropcluster --stop 14 mainRemove PostgreSQL packages
After you drop clusters you no longer need:
sudo apt purge postgresql postgresql-14
sudo apt autoremoveAdjust package names if you installed a different major from PGDG. For broader cleanup patterns, see how to remove software on Ubuntu.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
psql: connection to server failed on 127.0.0.1 |
Cluster down or not listening on TCP | Run pg_lsclusters; start with sudo systemctl start postgresql@14-main if Status is down |
PostgreSQL cluster shows down |
Failed upgrade, disk full, or port conflict | Read /var/log/postgresql/postgresql-14-main.log; free disk space; ensure nothing else uses port 5432 |
Peer authentication failed for user "postgres" |
Running psql as your login user without -h |
Use sudo -u postgres psql for local admin, or connect with -h 127.0.0.1 and a role that has a password in pg_hba.conf |
password authentication failed |
Wrong password or missing scram-sha-256 rule |
Run sudo -u postgres psql, then \password appuser at the psql prompt (avoids putting the password in shell history). Confirm pg_hba.conf uses scram-sha-256 for that connection. |
Remote client cannot reach port 5432 |
Host UFW, cloud security group, wrong listen_addresses, or pg_hba mismatch |
Confirm `ss -lntp |
no pg_hba.conf entry for host … |
Client IP, database, or role does not match any rule | Add a narrowly scoped host (or hostssl) line for that subnet, then sudo systemctl reload postgresql@14-main |
Summary
Ubuntu 22.04 makes PostgreSQL 14 the default apt install postgresql choice, and pg_lsclusters is the quickest way to see whether the real cluster (for example 14/main on port 5432) is online. For day-to-day use, create an application role with createuser --pwprompt and a matching database with createdb --owner, then prove TCP auth with psql -h 127.0.0.1.
When clients connect from other machines, set listen_addresses to specific IPs, restart the cluster, add a narrow host … scram-sha-256 line in pg_hba.conf, and allow only your subnet through UFW. Skip MD5 and 0.0.0.0/0 catch-all rules — prefer SCRAM-SHA-256 over legacy MD5 for TCP connections, and scope network rules to trusted clients only.
Add the PGDG repository with /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh when you need PostgreSQL 16, 18, or other non-default majors. On Ubuntu 22.04, the distro repository delivers PostgreSQL 14 — plan upgrades deliberately when applications require newer features.
References
- PostgreSQL: Linux downloads (Ubuntu)
- Ubuntu Server documentation: PostgreSQL
- PostgreSQL documentation: Client authentication
- Ubuntu Manpages: pg_lsclusters(1)
Frequently Asked Questions
Is PostgreSQL 14 still supported in 2026?
Ubuntu 22.04 still ships PostgreSQL 14 in apt, but upstream PostgreSQL 14 community support ends November 12, 2026. Ubuntu may continue Jammy package updates on a different schedule. Existing 14 deployments can follow this guide; new projects should evaluate PostgreSQL 16 or 18 from PGDG before choosing 14.
Do I need the PostgreSQL official repository on Ubuntu 22.04?
Use Ubuntu’s repository when PostgreSQL 14 meets your needs. Add PGDG when you require PostgreSQL 16 or 18, multiple majors on one server, or packages only published in PGDG.
What is the difference between the postgres Linux user and the postgres database role?
The Linux postgres account runs server processes and owns data files. The PostgreSQL postgres role is the database superuser. sudo -u postgres psql uses peer authentication to map the Unix account to that role locally without a password.
Where are postgresql.conf and pg_hba.conf on Ubuntu?
On Ubuntu 22.04 with PostgreSQL 14, they are /etc/postgresql/14/main/postgresql.conf and /etc/postgresql/14/main/pg_hba.conf.

