PostgreSQL Get Started

Learn how to install PostgreSQL, start the server, and connect to it using the psql command-line client.

Installing PostgreSQL

PostgreSQL can be installed on Windows, macOS, and Linux using official installers or package managers. On Windows, the EDB installer bundles the server, pgAdmin (a graphical management tool), and command-line tools. On macOS, Homebrew provides a quick installation path, and on Linux, most distributions ship PostgreSQL through their native package managers such as apt or yum.

  • Windows: download the installer from postgresql.org and run the setup wizard
  • macOS: run brew install postgresql@16
  • Ubuntu/Debian: run sudo apt install postgresql postgresql-contrib
  • Docker: run a containerized instance without installing anything locally
Note: During installation you will be asked to set a password for the default superuser account, which is named "postgres". Remember this password, as you will need it to connect.

Starting the PostgreSQL Server

On most systems, PostgreSQL installs as a background service that starts automatically after installation or system boot. You can verify the service status or start it manually depending on your operating system's service manager.

Check service status on Linux

sudo systemctl status postgresql

Connecting with psql

psql is PostgreSQL's official interactive command-line client. It lets you run SQL statements directly against a database, list tables, inspect schemas, and manage users. To connect, you typically specify a username, a database name, and a host.

Connect to the default database

psql -U postgres -d postgres -h localhost

List all databases from within psql

\l

Create and switch to a new database

CREATE DATABASE school;
\c school
psql CommandPurpose
\lList all databases
\c dbnameConnect to a different database
\dtList tables in the current database
\d tablenameDescribe a table's columns and types
\qQuit the psql session
Note: Backslash commands like \l and \dt are psql-specific shortcuts, not standard SQL. They only work inside the psql client, not in application code or other SQL tools.

Exercise: PostgreSQL Get Started

What is the default port PostgreSQL listens on?