apt cannot find the postgresql package on Ubuntu
E: Unable to locate package postgresqlShort answer
apt can only install packages that appear in its package index. The error means the index is empty or stale, or the version you named is not in your Ubuntu release. Run apt update first. For a version Ubuntu does not ship, add the official PostgreSQL apt repository with the pgdg script, then install postgresql-NN.
Fix it fast
1Find out which cause you have
Ask apt which postgresql packages it knows about. No output means the index has nothing for that name.
apt search --names-only '^postgresql'2Match what you see, then jump to its fix
What does the error look like?
E: Unable to locate package postgresqlE: Unable to locate package postgresql-18
Cause 1 of 2
The package index has not been downloaded or is stale
1Check
apt update downloads the package information from all configured sources, and other commands work from that data.
sudo apt update2Fix
Refresh the index, then install. Ubuntu's own postgresql package installs the one version that release supports.
sudo apt update
sudo apt install postgresql3Verify
Check that the server package is installed.
dpkg -l | grep postgresqlConfirmed by manpages.ubuntu.com
Cause 2 of 2
You asked for a version that Ubuntu does not ship
1Check
The PostgreSQL project explains that Ubuntu fixes one version for each release. Other versions come from the PostgreSQL Apt Repository.
apt-cache policy postgresql2Fix
Add the official repository with the script from postgresql-common, then install the version you want.
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
sudo apt update
sudo apt install postgresql-183Verify
Confirm the installed server package.
apt list --installed 2>/dev/null | grep postgresqlConfirmed by postgresql.org
How do I stop it coming back?
- Run apt update in every image build or setup script before apt install.
- Pin the PostgreSQL version in your setup script and use the official repository when it is not the Ubuntu default.
- Check that your Ubuntu release is in the repository's supported list before adding it.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Databases
DBeaver cannot run pg_restore: the utility is not found in the client home
Utility 'pg_restore' not found in client home '/usr/lib' (/usr/lib)
DBeaver runs pg_restore from a local PostgreSQL client folder; it does not bundle one.
3 causes, each with a fixDatabases
Django cannot connect to PostgreSQL: connection refused on port 5432, locally or in Docker
Is the server running on that host and accepting TCP/IP connections?
Connection refused means nothing accepted the connection at the host and port Django used.
4 causes, each with a fixDatabases
manage.py test stops with 'permission denied to create database' on PostgreSQL
django.db.utils.ProgrammingError: permission denied to create database
Django creates a separate database called test_ plus your database name, using the DATABASES USER.
2 causes, each with a fixDatabases
An EC2 instance cannot reach its RDS database
Could not open connection to the host, on port <port>: Connect failed
RDS denies all access until a security group allows it.
3 causes, each with a fixDatabases
IntelliJ IDEA Test Connection fails for a PostgreSQL data source
Connection to localhost:5432 refused.
When Test Connection fails, work from the bottom up.
4 causes, each with a fixDatabases
pg_dump refuses to run: server version is newer than pg_dump
pg_dump: detail: server version: 17.2; pg_dump version: 14.13
pg_dump will not dump a server whose major version is newer than its own.
3 causes, each with a fix