The shell says initdb: command not found after installing PostgreSQL
initdb: command not foundShort 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.
Fix it fast
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/postgresql2Match what you see, then jump to its fix
- 1
dpkg lists no postgresql-NN server package, and /etc/postgresql does not existThe server package is not installedFix - 2
The server package is installed, but initdb is not found in PATHYou want another cluster: use pg_createcluster, not initdbFix - 3
initdb refuses to start while you are rootinitdb is run as rootFix
- 1
What does the error look like?
initdb: command not foundbash: 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 postgresql2Fix
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 postgresql3Verify
The server package is now listed.
dpkg -l | grep -E 'postgresql'Confirmed by wiki.postgresql.org
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 -402Fix
Create the cluster with the wrapper. Options after -- go straight to initdb.
sudo pg_createcluster 16 main2 -- --encoding=UTF83Verify
The config folder for the new cluster now exists.
ls /etc/postgresql/16/main2Confirmed by manpages.debian.org
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 -un2Fix
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/data23Verify
initdb ends with a line telling you how to start the server.
sudo -u postgres ls /var/lib/postgresql/data2Confirmed by postgresql.org
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
Databases
The shell says psql: command not found
psql works on the database host but not on the application server
The shell cannot find a psql program.
2 causes, each with a fixDatabases
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 fix