Install PostgreSQL and Connect to It Locally

Install PostgreSQL, start the server, create a database and role, and connect with psql from your terminal.

beginner10 minmacos · windows · linuxUpdated August 2026
#postgresql#databases#sql#psql

Requirements

  • Terminal access
  • Admin/sudo access

Choose your platform

0/3 steps · 0%
01

Install and start PostgreSQL

bash
$ brew install postgresql@16
$ brew services start postgresql@16

brew services keeps Postgres running in the background and restarts it automatically after a reboot.

02

Create a database

bash
$ createdb {{DB_NAME}}

Note

Homebrew's Postgres uses your macOS username as the default superuser role, so no extra setup is needed here.

03

Connect and verify

bash
$ psql {{DB_NAME}}
sql
$ SELECT version();

Success

You're connected. Exit with \q.

Troubleshooting

By default, psql tries to authenticate using a database role that matches your OS username — if that role was never created, the connection is rejected before it even checks a password.

bash
$ sudo -u postgres createuser --interactive

Or connect explicitly as an existing role: psql -U postgres.