Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

apt cannot find the postgresql package on Ubuntu

ErrorE: Unable to locate package postgresql

Short 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.

  • Applies to Ubuntu 24.04, PostgreSQL 17, PostgreSQL 18
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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'
  2. 2Match what you see, then jump to its fix

    1. 1A freshly created server, container or image where apt update was never runThe package index has not been downloaded or is staleFix
    2. 2postgresql-NN fails but plain postgresql installs, or the version is newer than the release's snapshotYou asked for a version that Ubuntu does not shipFix

What does the error look like?

  • E: Unable to locate package postgresql
  • E: 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 update

2Fix

Refresh the index, then install. Ubuntu's own postgresql package installs the one version that release supports.

sudo apt update
sudo apt install postgresql

3Verify

Check that the server package is installed.

dpkg -l | grep postgresql

Confirmed by manpages.ubuntu.com

Did this fix it?

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 postgresql

2Fix

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-18

3Verify

Confirm the installed server package.

apt list --installed 2>/dev/null | grep postgresql

Confirmed by postgresql.org

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.