Skip to content
Visual explainer 2h to learn 3 steps

Installing PostgreSQL & using psql

Run Postgres locally or in Docker and navigate it with psql meta-commands.

By Invicly Tech, , in SQL & PostgreSQL. About 9 minutes to read.

Topic 1 of 30

In short

PostgreSQL is a database server: a program that stores data and answers SQL queries over a network connection. psql is its command-line client, where you type SQL plus backslash meta-commands to explore databases and tables.

Explained simply

A database is a program whose only job is to keep data safe and answer questions about it. PostgreSQL (often just "Postgres") is one of the most popular free databases. It runs in the background as a server, and other programs talk to it over a network connection.

Think of Postgres as a library with a strict librarian. You never walk into the shelves yourself. You write a request on a slip (a SQL query), hand it over the counter, and the librarian brings back exactly what you asked for.

psql is the counter where you hand over those slips. It is a small text program that connects to the server, sends what you type, and prints the answer as a neat table. Apps like pgAdmin and DBeaver do the same job with buttons and windows.

To learn, you need three things: a running server, a client to connect with, and a database to play in. Docker gives you the server in one command, psql is the client, and CREATE DATABASE gives you a safe sandbox you can delete and recreate any time.

Press Play on a diagram to walk through it one step at a time.

LayersFrom psql to disk
  1. 1
    Client (psql, app)

    Sends SQL over a connection, usually port 5432

  2. 2
    Backend process

    One server process per connection runs your query

  3. 3
    Shared buffers

    In-memory cache of table and index pages

  4. 4
    WAL

    Write-ahead log records changes before data files

  5. 5
    Data files on disk

    Tables and indexes stored as files

MapHow a server is organised
Postgres server (cluster)
  • Database: shop
    • Schema: public
    • Tables: customers, orders
  • Database: postgres
    • Default admin database
  • Roles
    • postgres (superuser)
    • app_user
SequenceWhat happens when you run one query in psql
You
psql
Postgres server
  1. 1Type SELECT ... ;
    You to psql: Type SELECT ... ;
  2. 2Send SQL over the connection
    psql to Postgres server: Send SQL over the connection
  3. 3Parsed, planned, run: rows + status (SELECT 3)
    Postgres server to psql: Parsed, planned, run: rows + status (SELECT 3)
  4. 4Print a formatted table
    psql to You: Print a formatted table

In depth

What a connection actually needs

Every client, whether psql or your app, needs the same five facts to connect: the host (which machine), the port (which door, 5432 by default), the user (a role name), the password, and the database name. These are often written together as one connection string, for example postgresql://postgres:secret@localhost:5432/shop.

If any one is wrong you get a clear error. "Connection refused" means nothing is listening on that host and port. "Password authentication failed" means the server is there but rejected your login. "Database does not exist" means you reached the server but asked for the wrong database.

  • host: where the server runs
  • port: 5432 unless changed
  • user + password: who you are
  • dbname: which database to open
MapReading a connection string
postgresql://postgres:secret@localhost:5432/shop
  • user
    • postgres
  • password
    • secret
  • host
    • localhost
  • port
    • 5432
  • dbname
    • shop

Docker vs a local install

A local install puts Postgres on your machine as a system service. It starts with your computer and stores data in a system folder. A Docker container keeps Postgres in its own box. You can run version 16 and 17 side by side on different ports and throw them away when done.

One catch with Docker: if you delete the container, its data goes too, unless you mount a volume with -v pgdata:/var/lib/postgresql/data. A volume is a folder Docker keeps outside the container, so your tables survive restarts and upgrades.

Getting around psql quickly

psql has two kinds of input. SQL statements go to the server and must end with a semicolon. Backslash commands are handled by psql itself and end at the line break.

A few settings make life much easier. \x auto switches to a vertical layout when rows are too wide. \timing shows how long each query took. \e opens your last query in an editor so you can fix long statements. The up arrow recalls history, and Tab completes table and column names.

  • \x auto: readable wide rows
  • \timing: show query time
  • \e: edit the last query in an editor
  • \i file.sql: run a script file
  • \conninfo: show who and where you are

How the prompt tells you what is happening

The psql prompt changes to show its state. shop=# means you are in database shop as a superuser, and shop=> means a normal user. If the prompt becomes shop-# you have started a statement but not finished it, usually because the semicolon is missing. shop(# means a bracket is still open, and shop'# means a quote is still open.

When stuck, type the missing character, or press Ctrl+C to throw away the half-written statement and start again.

MapWhat the psql prompt is telling you
psql prompt
  • shop=#
    • Ready, superuser
  • shop=>
    • Ready, normal user
  • shop-#
    • Statement not finished
    • Usually a missing ;
  • shop(#
    • A bracket is still open
  • shop'#
    • A quote is still open

Learn it in 3 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Run Postgres in Docker or locally~40m
  2. 2
    Connect with psql~40m
  3. 3
    Use \l, \dt and \d meta-commands~40m

Key points

  • Docker is the quickest way to run Postgres locally for learning.
  • Connect with psql -h localhost -U postgres -d shop.
  • Meta-commands start with a backslash and are handled by psql, not the server.
  • SQL statements end with a semicolon; meta-commands do not.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
docker run --name pg -e POSTGRES_PASSWORD=secret -p 5432:5432 -d postgres:17
psql -h localhost -U postgres

-- inside psql
CREATE DATABASE shop;
\l              -- list databases
\c shop         -- connect to shop
\dt             -- list tables
\d customers    -- describe a table
\?             -- help on meta-commands
\q              -- quit

Starting a disposable practice server with saved data

Step 1: start Postgres in Docker with a named volume so data survives restarts. Step 2: connect with psql. Step 3: create a practice database and a tiny table. Step 4: check it with \dt and \d. If you break something, DROP DATABASE practice and start over in seconds.

bash
docker run --name pg -e POSTGRES_PASSWORD=secret \
  -p 5432:5432 -v pgdata:/var/lib/postgresql/data -d postgres:17

psql postgresql://postgres:secret@localhost:5432/postgres

-- inside psql
CREATE DATABASE practice;
\c practice
CREATE TABLE notes (id serial PRIMARY KEY, body text);
\dt
\d notes

Diagnosing a connection that will not work

You run psql -h localhost -U postgres and see "connection refused". Step 1: docker ps shows whether the container is running. Step 2: if it is not listed, docker ps -a shows it exited; docker logs pg shows why, often "port is already allocated" because another Postgres uses 5432. Step 3: map a different host port with -p 5433:5432 and connect with -p 5433. The error message always points at which of the five connection facts is wrong.

Quick reference

  • psql -h HOST -p 5432 -U USER -d DB (or one URL: postgresql://user:pass@host:5432/db)
  • \l databases, \c db switch, \dt tables, \d name describe, \dn schemas, \du roles
  • \x auto for wide rows; \timing to time queries; \q to quit
  • SQL ends with ; and goes to the server. Backslash commands are handled by psql
  • Prompt db-# means the statement is unfinished: add ; or press Ctrl+C
  • Docker data lives in the container unless you mount a volume with -v
  • SELECT version(); shows the exact server version

Common mistakes

  • Forgetting the semicolon, so psql waits silently for more input.
  • Another Postgres already using port 5432, so the container cannot bind.
  • Creating tables while connected to the wrong database.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1psql says "connection refused". What is the most likely cause?

Question 2

2Your prompt shows shop-# after pressing Enter. What does that mean?

Question 3

3Which command shows the columns and types of the table orders?

Question 4

4Why add -v pgdata:/var/lib/postgresql/data when running Postgres in Docker?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Connect to your server with psql, print the server version, list all databases and quit.

  2. Exercise 2medium

    Create a database called library, connect to it, create a table books with an id, title and year, insert two rows and show them.

  3. Exercise 3hard

    Run two Postgres versions (16 and 17) at once in Docker and prove from psql that you are talking to each one.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Do I need pgAdmin, or is psql enough?
psql is enough to learn and is available on every server you will ever touch. Graphical tools like pgAdmin or DBeaver are nice for browsing data, so use whichever you like alongside psql.
What is the default password for the postgres user?
There is none. You set it yourself, for example with POSTGRES_PASSWORD in Docker, or with \password inside psql on a local install.
Is PostgreSQL the same as SQL?
No. SQL is the language, and PostgreSQL is one database that speaks it. Most of what you learn carries over to MySQL or SQL Server, with small differences in extra features.

Go deeper

ShareXLinkedInWhatsApp