Sub topic 14
Fixing Common PostgreSQL Connection Errors
Assume you have PostgreSQL installed, basic terminal skills, and a database server that you are trying to reach with psql. A PostgreSQL connection needs four main values:
- Host: the computer running PostgreSQL, often
localhost. - Port: the network port, usually
5432. - User: a PostgreSQL role, such as
postgres. - Database: the database to open.
Start with a connection check
Run these commands from your terminal:
psql --version
pg_isready -h localhost -p 5432psql --version checks the client installation. pg_isready checks whether a server accepts connections at the specified host and port.
If the server is ready, connect explicitly:
psql -h localhost -p 5432 -U postgres -d postgresThe -h, -p, -U, and -d options set the host, port, user, and database. Being explicit helps you diagnose which setting is wrong.
Interpret the common errors
| Error message | Likely cause | First check |
|---|---|---|
| `connection refused` | The server is stopped or the port is wrong | Check the server status and port |
| `No such file or directory` | `psql` tried a local socket that does not exist | Add `-h localhost` |
| `password authentication failed` | The password is wrong for that role | Verify the role and password |
| `role "name" does not exist` | The requested PostgreSQL role is missing | Connect with an existing administrative role |
| `database "name" does not exist` | The database name is wrong or has not been created | Connect to an existing database |
| `no pg_hba.conf entry` | The server's client authentication rules reject the connection | Review the server access rules |
The server is not accepting connections
connection refused usually means no process is listening at the requested address. Start PostgreSQL using the service manager for your operating system, then test again with pg_isready.
If PostgreSQL listens on another port, discover the configured port or ask the administrator. Then use that port:
pg_isready -h localhost -p 5433
psql -h localhost -p 5433 -U postgres -d postgresA local connection without -h uses a Unix socket on Linux and macOS. If that socket is unavailable, force a TCP connection with -h localhost.
Fix authentication and naming errors
For password authentication failed, check that you used the intended role and that your password belongs to that role. A successful login to one database does not prove that another database exists or that the role can access it.
For role does not exist, list roles after connecting with an administrative account:
\duFor database does not exist, list databases:
\lConnect to a known database, often postgres, before creating or inspecting another database:
psql -h localhost -p 5432 -U postgres -d postgresA no pg_hba.conf entry error means PostgreSQL received the connection but found no matching client authentication rule. The rule must match the connection type, database, role, client address, and authentication method. An administrator may need to update pg_hba.conf and reload PostgreSQL.
Use a repeatable diagnosis
Exercise
pg_isready -h localhost -p 5432 reports that the server is ready, but psql -h localhost -p 5432 -U analyst -d firstdb says that role analyst does not exist. What should you investigate first?
Show answer
Investigate the role name. The server and network settings work, but PostgreSQL has no role named analyst, or you are connecting to a different server than expected.