Sub topic 5
Host, Port, User, and Database: What `psql` Connects To
This chapter assumes you have PostgreSQL installed, the server is running, and you are comfortable using a terminal. You should also know that a database stores data in tables and that SQL is used to work with that data.
When you run psql, you start a client program. The client connects to a PostgreSQL server. The connection needs four main pieces of information:
- Host: the computer running PostgreSQL.
- Port: the network endpoint where PostgreSQL listens.
- User: the PostgreSQL role used for authentication.
- Database: the database where your SQL commands run.
A PostgreSQL role is an account that can connect and perform permitted actions. A role is often called a user when it is used for a person's login.
The default connection
A typical local PostgreSQL installation listens on port 5432. The hostname localhost means “this computer.” The role postgres is commonly created during installation.
This command supplies the host, port, and user:
psql -h localhost -p 5432 -U postgresThe command does not name a database. In that case, psql usually tries to connect to a database with the same name as the user, such as postgres. You can provide the database explicitly:
psql -h localhost -p 5432 -U postgres -d postgresThe -d option means database. Once connected, the prompt normally shows the selected database, for example postgres=#.
A successful connection proves that the server accepted the network connection and authenticated the role. It does not prove that the role can access every database or table.
Host and port choices
For a local connection, use localhost when you want a TCP connection. You can also omit -h; on many systems, psql then uses a local Unix-domain socket instead of TCP.
These connections can behave differently:
psql -h localhost -p 5432 -U postgres -d postgres
psql -p 5432 -U postgres -d postgresThe first uses TCP. The second commonly uses a local socket. If one works and the other fails, the server may be listening on only one connection method, or authentication rules may differ.
A remote connection uses the server's hostname or IP address:
psql -h db.example.com -p 5432 -U appuser -d appdbRemote access also requires server configuration, network access, and a password or another approved authentication method.
Connection settings as environment variables
You can avoid repeating options by setting PostgreSQL environment variables:
export PGHOST=localhost
export PGPORT=5432
export PGUSER=postgres
export PGDATABASE=postgres
psqlpsql reads these variables when corresponding command-line options are absent. A command-line option takes precedence over the environment variable. This is useful for scripts, but inspect your environment when a connection unexpectedly targets the wrong server or database.
Common connection errors
| Error | What it usually means | First check |
|---|---|---|
| Connection refused | No server is listening at that host and port | Check the server status and port |
| Password authentication failed | The role or password is incorrect | Check `-U`, the password, and authentication rules |
| Database does not exist | The requested database name is wrong or has not been created | Check the database name |
Check the connection
After connecting, run a query that identifies the server:
SELECT version();The result includes the PostgreSQL version. This confirms that your session reached a PostgreSQL server and is ready for SQL work.
Exercise
Which command connects to the database firstdb on the local server as the role postgres over port 5432?
Show answer
psql -h localhost -p 5432 -U postgres -d firstdb