Sub topic 2
Connecting to a Database with psql
Prerequisites and the PostgreSQL connection model
This chapter assumes that you know basic SQL, can use a terminal, and understand tables, columns, primary keys, and foreign keys. PostgreSQL is installed locally, or you have access to a PostgreSQL server.
A PostgreSQL server is a running service that can contain several databases. A database contains schemas, and schemas contain tables, views, functions, and other objects. A psql connection opens one database at a time. You can inspect other databases only if your role has permission to connect to them.
Connecting with explicit options
The psql command-line client connects to PostgreSQL. Give it the server address, port, role, and database when you want the connection to be unambiguous:
psql -h localhost -p 5432 -U postgres -d mydbThese options have specific meanings:
| Option | Meaning | Example |
|---|---|---|
| `-h` | Server host name or address | `localhost` |
| `-p` | Server port | `5432` |
| `-U` | PostgreSQL role used for login | `postgres` |
| `-d` | Database to open | `mydb` |
If password authentication is enabled, psql prompts for the password. The password is hidden while you type it. Avoid putting passwords directly in shell commands because shell history and process listings may expose them.
Some connection details can come from environment variables such as PGHOST, PGPORT, PGUSER, and PGDATABASE. Explicit flags are useful while learning and when documenting a repeatable command.
Checking the connection
After psql starts, its prompt usually includes the database name. Do not rely only on the prompt. Run \conninfo to display the actual connection:
\conninfoA typical result identifies the database, role, host, and port. Commands beginning with a backslash are psql meta-commands. They are handled by the client rather than sent to PostgreSQL as SQL.
To leave the session, run:
\quitYou can also press Ctrl+D on most terminals.
> callout note > localhost means the local computer. It does not mean the database is available automatically. A PostgreSQL server must be running and listening on the selected port, and your role must have permission to connect.
Common connection failures
Read the error message for the layer that failed:
- Could not connect to server usually means the host, port, service, or network path is wrong.
- Connection refused often means PostgreSQL is stopped or is not listening on that port.
- Database does not exist means the server answered, but the
-ddatabase name is incorrect. - Role does not exist means the
-Uvalue is not a PostgreSQL role on that server. - Password authentication failed means the role and password did not authenticate successfully.
- Permission denied for database means the role exists but cannot connect to that database.
Confirm the host, port, database, role, and password separately. A successful connection proves that you can enter the database; it does not prove that you can read every table.
Exercise
Write a psql command that connects to database sales on host db.example.com, port 5433, using role reporter. Which command checks the resulting connection?
Show answer
Run psql -h db.example.com -p 5433 -U reporter -d sales, then run \conninfo at the psql prompt.