Connect with psql
A connection identifies a host, port, database, and role. Local installations may use socket connections, while remote deployments commonly use TCP. Authentication is controlled by server configuration, including pg_hba.conf for host-based access. Never assume that a connection works merely because the server process is running.
Use environment variables or a secret manager for credentials rather than embedding passwords in source code or shell history. Connection URIs are convenient, but credentials in a URI can be exposed in logs if handled carelessly.
Connect to a database
psql -h localhost -p 5432 -U app_user -d app_db
# Once connected:
\conninfo
\dt
\q💡 The command selects a host, port, user, and database. In psql,
\conninfo prints connection information, \dt lists tables visible in the current search path, and \q exits the client.Key Takeaways
- A connection includes host, port, role, and database.
- Authentication rules control who can connect.
- Protect credentials and avoid logging connection secrets.
Knowledge Check
Which psql meta-command lists tables in the current search path?