PostgreSQL
databasepostgresqlReferenceBeginner
Connect to PostgreSQL (psql)
Connect to a local or remote PostgreSQL database instance using the psql command-line client.
Verified: 2026-09-30
Tested on:postgres 16.x
Platforms:
linuxmacoswindows
#postgresql#psql#cli#database
Command
psql -h localhost -p 5432 -U postgres -d mydb
Short Description
psql is the interactive terminal-based frontend for PostgreSQL. It allows you to enter queries interactively, issue them to PostgreSQL, and inspect the query results.
Examples
1. Connect Using Connection URI
psql "postgresql://user:secret@localhost:5432/production_db?sslmode=require"2. Execute SQL File Directly
psql -U postgres -d mydb -f schema_migration.sql3. Run Single Command and Exit
psql -U postgres -d mydb -c "SELECT version();"