PostgreSQL cluster fails to start with pg_ctlcluster on Ubuntu or Debian
Failed to start PostgreSQL Cluster 16-main.Short answer
The systemd message only says the cluster did not start; the reason is in the cluster's own log. Read /var/log/postgresql, or start the cluster in the foreground to see the error on screen. Then fix what it names: a port already in use, data directory permissions, or shared memory and semaphore limits.
Fix it fast
1Find out which cause you have
Show the clusters (version, name, port, data directory), then print the end of the log of the failing one. Replace 16-main with your cluster.
pg_lsclusters sudo tail -n 30 /var/log/postgresql/postgresql-16-main.log2Match what you see, then jump to its fix
- 1
could not bind IPv4 address ...: Address already in useAnother process already holds the portFix - 2
The log complains about the data directory, usually after a restore, copy or mount changeData directory permissions are wrongFix - 3
could not create shared memory segment: Invalid argument, or could not create semaphores: No space left on deviceKernel limits for shared memory or semaphoresFix
- 1
What does the error look like?
Failed to start PostgreSQL Cluster 16-main.could not bind IPv4 address "127.0.0.1": Address already in useFATAL: could not create any TCP/IP sockets
Cause 1 of 3
Another process already holds the port
1Check
Two clusters, or a manually started server, may use the same port. Find who listens on 5432.
sudo ss -ltnp | grep 54322Fix
Stop the other server, or give this cluster a different port in its postgresql.conf, then start it.
sudo grep -n "^port" /etc/postgresql/16/main/postgresql.conf
sudo pg_ctlcluster 16 main start3Verify
The status command reports a running server.
sudo pg_ctlcluster 16 main statusConfirmed by postgresql.org
Cause 2 of 3
Data directory permissions are wrong
1Check
A cluster data directory must be owned by the PostgreSQL user and closed to everyone else. The expected modes are 0700 for directories and 0600 for files, or 0750 and 0640 when group read is allowed.
sudo ls -ld /var/lib/postgresql/16/main2Fix
Give the directory back to the postgres user and set the mode while the cluster is stopped.
sudo chown -R postgres:postgres /var/lib/postgresql/16/main
sudo chmod 0700 /var/lib/postgresql/16/main
sudo pg_ctlcluster 16 main start3Verify
The status command reports a running server.
sudo pg_ctlcluster 16 main statusConfirmed by postgresql.org
Cause 3 of 3
Kernel limits for shared memory or semaphores
1Check
The second message does not mean the disk is full. It means the System V semaphore limit is below what PostgreSQL asks for. The shared memory message applies when shared_memory_type is sysv.
sudo grep -n "shared_buffers\|max_connections\|shared_memory_type" /etc/postgresql/16/main/postgresql.conf2Fix
Start with fewer buffers or fewer connections, and raise the kernel limit for the long term.
# in postgresql.conf, then start the cluster again
max_connections = 50
shared_buffers = 128MB3Verify
The cluster starts and the log has no FATAL line.
sudo tail -n 5 /var/log/postgresql/postgresql-16-main.logConfirmed by postgresql.org
How do I stop it coming back?
- Run pg_lsclusters after every package upgrade to see which clusters exist and which ports they use.
- Do not copy a data directory without keeping its owner and mode.
- Keep one server per port and never start a second postgres by hand on a managed cluster.
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
PostgreSQL says postgresql.conf has errors and a setting does not apply
a changed setting does not take effect after pg_reload_conf()
PostgreSQL logs invalid settings and ignores them on reload, so a change can silently not apply.
3 causes, each with a fixDatabases
Cannot connect to PostgreSQL on port 5432 from another machine
FATAL: no pg_hba.conf entry for host "198.51.100.7", user "app", database "appdb"
Two settings guard remote access.
3 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 fix