Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

The shell says initdb: command not found after installing PostgreSQL

Errorinitdb: command not found

Short answer

Either the PostgreSQL server package is not installed, or it is installed but initdb is not on your PATH. On Debian and Ubuntu the postgresql-common tools wrap initdb, and pg_createcluster is the way to make a cluster there. Also, initdb refuses to run as root.

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

Fix it fast

  1. 1Find out which cause you have

    Ask the package manager which PostgreSQL packages are installed, and look at which cluster config folders exist.

    dpkg -l | grep -E 'postgresql'
    ls /etc/postgresql
  2. 2Match what you see, then jump to its fix

    1. 1dpkg lists no postgresql-NN server package, and /etc/postgresql does not existThe server package is not installedFix
    2. 2The server package is installed, but initdb is not found in PATHYou want another cluster: use pg_createcluster, not initdbFix
    3. 3initdb refuses to start while you are rootinitdb is run as rootFix

What does the error look like?

  • initdb: command not found
  • bash: initdb: command not found

Cause 1 of 3

The server package is not installed

1Check

The apt repository documentation names the postgresql and postgresql-NN packages as the way to install the server.

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

2Fix

Install the server package. The PostgreSQL project's own apt repository carries the current major versions; the postgresql meta-package follows the latest one, and postgresql-NN pins a major version.

sudo apt update
sudo apt install postgresql

3Verify

The server package is now listed.

dpkg -l | grep -E 'postgresql'

Confirmed by wiki.postgresql.org

Did this fix it?

Cause 2 of 3

You want another cluster: use pg_createcluster, not initdb

1Check

On Debian and Ubuntu the postgresql-common tools wrap initdb. When the data directory does not exist yet, pg_createcluster runs initdb for you and puts the config in /etc/postgresql/<version>/<name>/.

man pg_createcluster | head -40

2Fix

Create the cluster with the wrapper. Options after -- go straight to initdb.

sudo pg_createcluster 16 main2 -- --encoding=UTF8

3Verify

The config folder for the new cluster now exists.

ls /etc/postgresql/16/main2

Confirmed by manpages.debian.org

Did this fix it?

Cause 3 of 3

initdb is run as root

1Check

initdb must run as the user that will own the server process. The server cannot run as root, so initdb refuses to run as root.

id -un

2Fix

Run it as the account that will own the server process, with an explicit data directory, using the full path to the initdb binary you installed.

sudo -u postgres /path/to/initdb -D /var/lib/postgresql/data2

3Verify

initdb ends with a line telling you how to start the server.

sudo -u postgres ls /var/lib/postgresql/data2

Confirmed by postgresql.org

Did this fix it?

How do I stop it coming back?

  • On Debian and Ubuntu create clusters with pg_createcluster rather than raw initdb.
  • Always give initdb an explicit -D directory, or set PGDATA.
  • Check the major version when you have more than one installed, since each has its own binaries.

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.