Installing PostgreSQL & Connecting with psql
10 min read
Installing PostgreSQL & Connecting with psql
The fastest way to get Postgres running locally is via your OS package manager. On macOS the recommended approach is Homebrew; on Ubuntu/Debian use apt.
bash
# macOS
brew install postgresql@16
brew services start postgresql@16
# Ubuntu / Debian
sudo apt update && sudo apt install -y postgresql postgresql-contrib
sudo systemctl start postgresql
sudo systemctl enable postgresqlAfter installation Postgres creates a system user named postgres. Switch to it and launch the interactive shell psql to verify everything is working.
bash
# Linux — switch to the postgres system user
sudo -i -u postgres psql
# macOS — the default superuser is your OS username
psql postgresEssential psql Meta-Commands
\l— list all databases\c dbname— connect to a database\dt— list tables in the current schema\d tablename— describe a table (columns, types, constraints)\dn— list schemas\timing— toggle query execution time display\q— quit
psql reads a ~/.psqlrc file on startup. A popular trick is to add \set PROMPT1 '%[%033[1;32m%]%n@%/%[%033[0m%]%# ' for a coloured prompt showing the current user and database.
bash
# Connect with explicit flags
psql -h localhost -p 5432 -U myuser -d mydb
# Run a single query and exit
psql -U myuser -d mydb -c 'SELECT version();'Ready to test yourself?
Practice PostgreSQL— quiz & coding exercises