Connecting to PostgreSQL Using psql: The Definitive Guide

Introduction to psql psql is PostgreSQL's powerful command-line client that lets you: Execute SQL queries Manage databases Administer users Import/export data And much more - all from your terminal! Whether you're a developer, DBA, or data analyst, mastering psql is essential for working efficiently with PostgreSQL. Basic Connection Methods 1. Simple Connection psql -U username -d database_name -h hostname -p port Example: psql -U postgres -d mydb -h localhost -p 5432 2. Connection Using Connection URI psql postgresql://username:password@hostname:port/database Example: psql postgresql://postgres:mypassword@localhost:5432/mydb 3. Connecting Without Specifying Database psql -U username This connects to a database with the same name as your username. Common Connection Options Option Description Example -U Username -U postgres -d Database name -d mydb ...