Sub topic 5
Host, port, user, and database: what psql connects to
This chapter explains the four values that decide where psql connects and who it connects as. It assumes you have PostgreSQL installed and a server running, and that you can start psql as the previous chapter showed. You should also be comfortable reading command-line options and setting environment variables in your shell.
The four connection parameters
Every connection to PostgreSQL is defined by four values:
| Parameter | psql option | Environment variable | Default if omitted |
|---|---|---|---|
| Host | `-h` or `--host` | `PGHOST` | Local Unix socket (Linux, macOS). On Windows, `localhost`. |
| Port | `-p` or `--port` | `PGPORT` | `5432` |
| User (role) | `-U` or `--username` | `PGUSER` | Your operating system user name |
| Database | `-d` or `--dbname` | `PGDATABASE` | The same as the user name |
The last default catches many beginners. If your OS user is alice and you run plain psql, PostgreSQL tries to connect as role alice to a database named alice. If that database does not exist, you get an error even though the server is healthy.
A fully explicit connection looks like this:
psql -h localhost -p 5432 -U postgres -d postgresEach part answers one question:
- Host is the machine that runs the server.
- Port is the network port the server listens on.
- User is the role you authenticate as.
- Database is the database inside the server you want to work in.
Host: socket or network
The host value decides which transport psql uses.
- If the host starts with a slash, such as
/var/run/postgresql, psql treats it as a directory that holds a Unix-domain socket file. This is a local-only channel managed by the operating system. - If the host is a name or an IP address, such as
localhost,127.0.0.1, ordb.example.com, psql opens a TCP connection to that machine. - If you omit the host on Linux or macOS, psql uses the default socket directory compiled into your client. Typical locations are
/var/run/postgresqlfor Debian and Ubuntu packages and/tmpfor many other builds, including Homebrew on macOS.
The two paths are not interchangeable. The server may accept socket connections but have TCP disabled, or the reverse. Authentication rules also differ between them, which is why psql -U postgres can work while psql -h localhost -U postgres asks for a password.
You can see the transport in use with the \conninfo meta-command:
\conninfoFor a socket connection, the output says you are connected "via socket in" a directory. For TCP, it names the host, the address, and the port.
Port: which server process
A single machine can run several PostgreSQL servers, and each needs its own port. The default is 5432. Linux packages sometimes start a second cluster on 5433 when you install a new major version next to an old one. If psql connects to a different version than you expected, check the port first.
Ask a running session which port it is using:
SELECT inet_server_port();Over a Unix socket, the port number is still part of the socket file name (for example .s.PGSQL.5432). If you set -p 5433 and omit -h, psql looks for the socket file with 5433 in its name.
User: the role you authenticate as
PostgreSQL calls accounts roles. The -U option names the role. It does not have to match your OS user. The role must exist on the server, and the server's rules must allow it to connect from your location to your chosen database. The next chapter covers roles in more detail.
Those rules live in a file named pg_hba.conf (host-based authentication). Each line matches a connection type (local for sockets, host for TCP), a database, a role, and an address, and then names an authentication method such as scram-sha-256, md5, peer, or trust. The server reads the lines from top to bottom and uses the first one that matches.
peer deserves attention because it is the default for local socket connections on many Linux packages. With peer, the server asks the OS who you are and requires your OS user name to match the requested role. That is why this works if you are logged in as postgres, and fails for any other OS user:
psql -U postgresThe usual fix is either to run the command as that OS user (sudo -u postgres psql) or to connect over TCP with -h localhost, which typically uses password authentication.
Database: where your session lives
A server holds many databases, but a session works in exactly one. The -d option picks it. Once connected, you can change databases with \c name, which closes the current connection and opens a new one, so the connection parameters apply again.
A fresh installation always contains a database named postgres, plus template0 and template1, which are used as templates when you create new databases. Connecting to postgres is the standard way to get in when you do not yet have a database of your own.
Verifying what you are connected to
Do not guess. Ask the server who you are and where you are:
SELECT current_user, current_database(), inet_server_addr(), inet_server_port();On a Unix socket connection, inet_server_addr() returns NULL because no IP address is involved. That is a useful sign that you are on a socket.
Three ways to write the same connection
psql accepts the parameters in several forms. All three commands below describe the same connection:
psql -h localhost -p 5432 -U postgres -d postgres
psql "host=localhost port=5432 user=postgres dbname=postgres"
psql "postgresql://postgres@localhost:5432/postgres"The second form is a key/value connection string, and the third is a URI. Use the URI or key/value form when you copy settings from an application's configuration or when you need extra options such as sslmode=require:
psql "postgresql://[email protected]:5432/postgres?sslmode=require"Quote these strings. Without quotes, the shell may treat characters such as ? and & as its own syntax.
Environment variables and precedence
Instead of repeating options, you can set environment variables for the shell session:
export PGHOST=localhost
export PGPORT=5432
export PGUSER=postgres
export PGDATABASE=postgres
psqlWhen the same parameter appears in more than one place, the most specific source wins. From highest to lowest priority:
- Command-line options and connection strings, such as
-horhost=.... - Environment variables, such as
PGHOST. - Built-in defaults.
This explains a common surprise. If PGPORT=5433 is exported in your shell profile and you forgot about it, psql goes to 5433 every time, while psql -p 5432 still reaches the default server. Use env | grep ^PG to see which variables are set.
Passwords
A password is not one of the four parameters, but it comes up in every TCP connection. psql prompts for it when the server asks for one. You can also store it in a .pgpass file, one line per connection in the form hostname:port:database:username:password. On Linux and macOS the file lives at ~/.pgpass and must have permissions 0600, or psql ignores it. On Windows it is %APPDATA%\postgresql\pgpass.conf.
echo "localhost:5432:*:postgres:your_password_here" >> ~/.pgpass
chmod 600 ~/.pgpassReading the result of a wrong parameter
Each wrong parameter produces a recognizable error. This chapter only maps them to causes. The troubleshooting chapter covers fixes.
| Message fragment | Likely wrong parameter |
|---|---|
| `connection refused` on a port | Port wrong, or no server listening on that host and port |
| `No such file or directory` and a socket path | Host omitted and the socket directory or port does not match the server |
| `role "x" does not exist` | User |
| `database "x" does not exist` | Database, often the default that equals your user name |
| `Peer authentication failed` | User does not match OS user over a socket |
| `password authentication failed` | User or password, over TCP |
Exercise
You run psql as OS user dana on a machine where only the roles postgres and app exist, and you see FATAL: role "dana" does not exist. Write one command that connects as app to a database named shop on the local server over TCP at the default port, and name the rule that caused the original error.
Show answer
Command: psql -h localhost -p 5432 -U app -d shop (the port may be left out because 5432 is the default). The original error happened because -U was omitted, so psql defaulted to the OS user name dana, and no such role exists on the server. The database would also have defaulted to dana.
Exercise
PGPORT=5433 is exported in your shell. You run psql -h localhost -U postgres -p 5432. Which port does psql use, and why?
Show answer
Port 5432. Command-line options take priority over environment variables, which take priority over built-in defaults.