Sub topic 14
Fixing common connection errors
This chapter assumes you can open a terminal, have installed PostgreSQL, and know the four connection parameters from earlier chapters: host, port, user, and database. It covers the errors you are most likely to meet when psql cannot connect, what each one means, and how to fix it.
How to read a connection error
A connection attempt passes through three stages. A failure at each stage produces a different kind of message.
- Reaching the server.
psqlopens a network connection (TCP) or a local socket. If nothing is listening, you get a "connection refused" or "No such file or directory" error. - Authenticating. The server decides whether your role is allowed to connect from your address, and checks the password if one is needed. Failures here mention
pg_hba.confor "password authentication failed". - Opening the database. The server checks that the database exists and that your role may connect to it. Failures here say
database "..." does not existorpermission denied.
The stage tells you where to look. A message that arrives instantly with "refused" is a stage 1 problem. A message that names a role is a stage 2 or 3 problem.
Start with the basics
Before you read a specific error, run the two checks from the earlier chapter on verifying the server.
pg_isready
pg_isready -h localhost -p 5432pg_isready prints one of these results:
| Output | Exit code | Meaning |
|---|---|---|
| `accepting connections` | 0 | The server is up. Look at authentication or database names. |
| `rejecting connections` | 1 | The server is running but is starting up or shutting down. Wait and retry. |
| `no response` | 2 | Nothing answered. The server is stopped, or the host or port is wrong. |
| `no attempt` | 3 | The parameters you gave were invalid, so no connection was tried. |
A result of no response means you should start the server or correct the host and port. Any other result means the server is alive and you should read the error text from psql.
Error: connection refused
psql: error: connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?This means no process is listening at that address and port. Common causes:
- The server is not running. Start it with the method for your system. On Linux with systemd, use
sudo systemctl start postgresql. On macOS with Homebrew, usebrew services start postgresql@16(use the version you installed). On Windows, start thepostgresql-x64-16service from the Services app. - A different port. Another installation, or a second cluster, may use 5433 or 5434. Try
-p 5433. - The server listens on another address. The
listen_addressessetting inpostgresql.confcontrols this. The default islocalhost, which is correct for a local practice setup. - A container or remote host. If PostgreSQL runs in Docker, the port must be published, for example with
-p 5432:5432.
Notice that the message above mentions ::1. That is the IPv6 form of localhost. If the server listens only on IPv4, psql may try both addresses and report the failure for one of them. Using -h 127.0.0.1 forces IPv4 and helps you tell which case you are in.
Error: socket file not found
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?When you give no -h option on Linux or macOS, psql connects through a Unix domain socket, a file on disk, instead of the network. This error means the file is missing. Either the server is stopped, or its socket lives in a different directory. Two fixes:
- Start the server, then run
pg_isreadyagain. - Connect over TCP instead:
psql -h localhost -U postgres.
Some installers place the socket in /tmp rather than /var/run/postgresql. If the server is running, psql -h /tmp points the client at that directory.
Error: password authentication failed
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: password authentication failed for user "postgres"The server was reached and the role exists, but the password was wrong. The server gives the same message when the role does not exist. That is deliberate, so attackers cannot discover valid role names.
Work through these checks:
- Confirm the user name.
-U postgresand-U Postgresare different, because role names are case sensitive when quoted. - Retype the password.
psqlshows no characters while you type. - Check whether a
PGPASSWORDenvironment variable or a~/.pgpassfile is supplying an old password. Either one silently overrides what you meant to use. - If you forgot the password, reset it from an account that can already connect. On Linux,
sudo -u postgres psqloften works through local peer authentication, and then you can run the statement below.
ALTER ROLE postgres WITH PASSWORD 'choose-a-new-password';Error: peer authentication failed
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "postgres"With peer authentication, the server checks that your operating system user name matches the requested database role. If you are logged in as alice and ask for -U postgres over the socket, it refuses, and no password is ever requested. You have three options:
- Run the command as the matching system user:
sudo -u postgres psql. - Connect over TCP with
-h localhost, which uses password authentication by default in most installs. - Create a role that matches your own system user name, so that plain
psqlworks.
CREATE ROLE alice WITH LOGIN SUPERUSER;
CREATE DATABASE alice OWNER alice;Run those statements as postgres, replacing alice with your system user name. Granting SUPERUSER is acceptable on a private practice machine and a poor habit anywhere else.
Error: no pg_hba.conf entry
FATAL: no pg_hba.conf entry for host "192.168.1.20", user "postgres", database "firstdb", no encryptionThe file pg_hba.conf (host-based authentication) lists which combinations of connection type, database, role, and client address the server accepts, and by what method. This message means that no line matches your connection. You will mostly see it when connecting from another machine or from a container. To find the file, ask the server:
SHOW hba_file;A line that allows password login from one local network looks like this:
host all all 192.168.1.0/24 scram-sha-256After editing pg_hba.conf, reload the configuration without a restart by running SELECT pg_reload_conf(); as a superuser. The server reads rules from top to bottom and uses the first match, so a broad reject line above your new line will still block you. For a local practice setup you rarely need to edit this file. If you do, keep the address range narrow.
Error: database does not exist
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: database "firstdb" does not existAuthentication succeeded, so the server is fine. You named a database that is not in the cluster. Typical reasons are a typo, having already run DROP DATABASE firstdb; during cleanup, or skipping CREATE DATABASE earlier in the tutorial.
If you give no database name, psql uses a database with the same name as your role. A role named alice therefore tries to open a database named alice, which may not exist. Connect to the always-present postgres database and look around:
psql -h localhost -U postgres -d postgres -c "\l"The -c option runs one command and exits. If firstdb is missing from the list, create it with CREATE DATABASE firstdb;.
Error: role does not exist
FATAL: role "alice" does not existIf you omit -U, psql uses your operating system user name. On a fresh install that name has no matching role, so the server refuses. Pass -U postgres explicitly, or create the role as shown in the peer authentication section.
Error: permission denied
ERROR: permission denied for table booksThis one appears after you connect, not during the connection. The role you are using has no privilege on that object. On a practice database, the simplest explanation is that you connected as a different role from the one that created the table. Check who you are and who owns the table:
SELECT current_user;
\dtThe \dt output includes an Owner column. Either reconnect as the owner or have the owner run GRANT SELECT, INSERT, UPDATE, DELETE ON books TO alice;.
Quick reference
| Message fragment | Stage | First thing to try |
|---|---|---|
| `Connection refused` | Reach | Run `pg_isready`, then start the server or fix the port |
| `No such file or directory` (socket) | Reach | Start the server, or use `-h localhost` |
| `password authentication failed` | Authenticate | Check user name and password, `PGPASSWORD`, `~/.pgpass` |
| `Peer authentication failed` | Authenticate | Use `sudo -u postgres psql` or `-h localhost` |
| `no pg_hba.conf entry` | Authenticate | Review `pg_hba.conf` rules and reload |
| `database ... does not exist` | Open | List databases with `\l`, then create or correct the name |
| `role ... does not exist` | Authenticate | Pass `-U postgres` or create the role |
| `permission denied` | After connect | Check `current_user` and the table owner |
Exercises
Exercise
You run psql -U postgres on Linux and see Peer authentication failed for user "postgres". The server is running. Name two ways to connect successfully.
Show answer
Run the command as the matching operating system user with sudo -u postgres psql, or connect over TCP with psql -h localhost -U postgres, which uses password authentication instead of peer authentication.
Exercise
pg_isready prints no response. Does that point to a wrong password? What do you check next?
Show answer
No. A wrong password can only be reported after the server answers, so no response means the server is not reachable. Check that the server is started, then check that the host and port are correct, for example by trying another port such as 5433.
Exercise
You finished the cleanup chapter, which dropped firstdb. Today psql -h localhost -U postgres -d firstdb fails. What is the error, and what do you do?
Show answer
The error is FATAL: database "firstdb" does not exist. Connect to the postgres database with psql -h localhost -U postgres -d postgres, then run CREATE DATABASE firstdb; if you want it again.