Rana Maninder Singh
Lesson 3 of 8Setup Guide

Installation: PostgreSQL & pgAdmin

Lesson opener

In one line: Install the PostgreSQL server, confirm it is running, then use pgAdmin only to register that server, create a lab database, and make one scratch table. Then repeat that connection in psql and in Python (psycopg2 / SQLAlc.

Why it matters: This shows up at work when you need to install the PostgreSQL server, confirm it is running, then use pgAdmin only to register that server, create a lab database, and make one scratch table. Then repeat that connection i

Before this lesson: MySQL install

Before you start

ResourceLink
PreviousMySQL install
NextPower BI install
Time~25 minutes
What you needA computer where you can install software (Windows steps below; Mac notes included)

What you will learn

Install the PostgreSQL server, confirm it is running, then use pgAdmin only to register that server, create a lab database, and make one scratch table. Then repeat that connection in psql and in Python (psycopg2 / SQLAlchemy) — the GUI hides the host, port, database, and user that production logs show.

pgAdmin is a GUI client. It does not include the database engine. Registering a server in pgAdmin before PostgreSQL is running will fail — that is a connection problem, not a “create server” problem.

Two passwords — do not mix them up

PasswordWhen you set itWhat it unlocks
postgres superuserDuring PostgreSQL installationThe database. This is what the Connection tab wants.
pgAdmin master passwordFirst time you open pgAdminpgAdmin’s saved-password vault on your machine. It is not a database login.

Host, port, and database name are connection details. Authentication is the username plus the postgres password. Those are different fields on purpose.


Tutorial 1 — Install the server, then prove it is running

  1. Download the PostgreSQL installer from postgresql.org/download (Windows: the EDB installer).
  2. Include PostgreSQL Server, pgAdmin 4, and Command Line Tools.
  3. Set the postgres password and store it locally (never commit it to git). Leave the port at 5432 unless you already know why you would change it.
  4. Finish the installer. It registers PostgreSQL as a background service and starts it.

Confirm the engine is accepting connections before you open pgAdmin:

pg_isready -h localhost -p 5432

You want: localhost:5432 - accepting connections.

If pg_isready is not found, the installer did not put bin on your PATH. The tools live under C:\Program Files\PostgreSQL\<version>\bin. You can also open Services and look for a service whose name starts with postgresql — status should be Running.

Mac: Homebrew installs are checked with brew services list. Postgres.app is a different install; use its own “running” indicator, then the same pg_isready check.

If this check fails, stop. Creating a pgAdmin server object will not start PostgreSQL.


Tutorial 2 — Open pgAdmin (desktop, not a guessed URL)

Start pgAdmin 4 from the Start menu. The desktop app opens its own window (often a browser tab on a local port it chose). Set the master password if this is the first launch. That password only unlocks saved logins inside pgAdmin.

Do not open http://localhost:5050 as the default. Port 5050 is a common Docker mapping (-p 5050:80) for pgAdmin in a container. A normal Windows desktop install does not listen there. If you did install pgAdmin in Docker, then 5050 may be right — only because you mapped it.


Tutorial 3 — Server group (optional)

Right-click Servers → Create → Server Group. Name it something like Local Dev.

This is a folder in the tree. It does not create a database or a login. Skip it if you prefer a flat list.


Tutorial 4 — Register the server that is already running

Right-click your group (or Servers) → Create → Server.

General tab:

Connection tab:

FieldValue
Host name/addresslocalhost (or 127.0.0.1)
Port5432
Maintenance databasepostgres
Usernamepostgres
PasswordThe postgres password from installation — not the pgAdmin master password

Save. A failed connection means the service is stopped, the port is wrong, or the password is the wrong one of the two. It does not mean you skipped a “create server” button.


Tutorial 5 — Create the lab database

Expand Local PostgreSQL → right-click Databases → Create → Database.

Save.

You can do the same in the Query Tool while connected to the postgres database:

CREATE DATABASE learnings_pg;

CREATE DATABASE cannot run inside a transaction block. In pgAdmin, run it as its own statement against the maintenance database postgres, not from inside a half-written script.


Tutorial 6 — Schemas, then one scratch table

Expand learnings_pg → Schemas. public is already there. You do not create a schema for this lab. A custom schema is a namespace you add later, when you need one — not a setup step.

Right-click public → Create → Table only to see the GUI once.

General: name the table lab_notes (a scratch table — the course tables customers and sales are created with SQL in pg1, so a GUI typo here does not block the track).

Columns:

NameTypeNotes
idserialMark as primary key. serial is the older auto-number spelling; pg2 shows the newer IDENTITY form.
titlevarcharLength 100. Not null.

Save.

Same table in SQL, if you would rather see the statement the GUI hid:

CREATE TABLE lab_notes (
  id    SERIAL PRIMARY KEY,
  title VARCHAR(100) NOT NULL
);

Run that while connected to learnings_pg, not to postgres.


Tutorial 7 — Same connection, without the GUI

pgAdmin is a reasonable first window. It is a poor daily tool.

A production failure looks like a connection string and a query in a log, not a right-click. The GUI hid four facts: host, port, database, user — plus the password you typed. Learn those in a client that prints them back.

Two clients matter. psql is the command line — the rest of this track (pg1 onward) uses it every lesson. Python (psycopg2, or SQLAlchemy on top of it) is how a script or a notebook talks to the same server. There is no later chapter for that Postgres driver. The Python SQL lesson on this site (Python & SQL) is MySQL (pymysql). Do not wait for a pgAdmin chapter there.

CLI — psql

This is the same registration you just did, written out:

psql -h localhost -p 5432 -U postgres -d learnings_pg
FlagWhat the GUI called it
-h localhostHost name/address
-p 5432Port
-U postgresUsername
-d learnings_pgThe database you created (not the maintenance database)

It prompts for the postgres password, not the pgAdmin master password. You should see learnings_pg=>. Then:

\conninfo
SELECT * FROM lab_notes;
\q

\conninfo is the check the GUI never forced: it prints host, port, user, and database. If those are wrong, every later query is about the wrong server.

Python — psycopg2

pip install psycopg2-binary

psycopg2-binary is the install that works without a C compiler. The import name is still psycopg2. The parameter is dbname, not database — that mismatch is a common first error.

# requires: postgresql
import psycopg2

conn = psycopg2.connect(
    host="localhost",
    port=5432,
    dbname="learnings_pg",
    user="postgres",
    password="your_postgres_password",
)
cur = conn.cursor()
cur.execute("SELECT current_database(), current_user")
print(cur.fetchone())  # ('learnings_pg', 'postgres')
cur.close()
conn.close()

Do not commit that password. For anything you keep, read it from the environment:

# requires: postgresql
import os
password = os.environ["PGPASSWORD"]

Python — SQLAlchemy

Same four facts, one URL. The driver name in the URL is required. postgresql:// with no driver is the thing that “works on someone else’s machine.”

pip install sqlalchemy psycopg2-binary
# requires: postgresql
import os
from sqlalchemy import create_engine, text

url = (
    "postgresql+psycopg2://postgres:"
    + os.environ["PGPASSWORD"]
    + "@localhost:5432/learnings_pg"
)
engine = create_engine(url)
with engine.connect() as conn:
    print(conn.execute(text("SELECT current_database(), current_user")).one())

If this fails, debug the URL — host, port, database, user — not a pgAdmin screenshot. That is the mechanic a GUI hides and a production log will not.


Essential commands

Command / actionWhat it does
pg_isready -h localhost -p 5432Checks that the server is accepting connections — do this before pgAdmin
Services → postgresql… RunningWindows check that the engine service is up
Register Server → ConnectionPoints pgAdmin at an engine that already exists
CREATE DATABASE learnings_pg;Creates the lab database (from the postgres database)
public schemaAlready exists — do not create one for this lab
CREATE TABLE lab_notes (…)Scratch table so you have seen the SQL the GUI generated
psql -h localhost -p 5432 -U postgres -d learnings_pgThe Connection tab as a command. Password prompt is the postgres user
\conninfoPrints host, port, user, database — confirm before you query
psycopg2.connect(host=…, port=5432, dbname=…, user=…)Same facts from Python. Parameter is dbname
postgresql+psycopg2://user:pass@localhost:5432/learnings_pgSQLAlchemy URL. Name the driver

Practice

Do this now:

  1. Run pg_isready -h localhost -p 5432 and read the line it prints.
  2. Register Local PostgreSQL in pgAdmin with host localhost, port 5432, maintenance database postgres, user postgres.
  3. Create database learnings_pg.
  4. Confirm schema public already exists, then create lab_notes (GUI or SQL).
  5. Quit pgAdmin. Connect with psql -h localhost -p 5432 -U postgres -d learnings_pg, run \conninfo, then SELECT * FROM lab_notes;. An empty result is success.
  6. Optional: the same SELECT from psycopg2 or SQLAlchemy, using PGPASSWORD rather than a password in the file.

Where This Connects

Next skill: Postgres orientation & psql — stay in psql. Daily commands are \conninfo, \l, \dt, \d.

Python: the snippet above is the Postgres connection. Python & SQL is the longer version of that idea, but it targets MySQL (pymysql), not psycopg2.

Sibling install: MySQL install is a different engine (Workbench, database learnings_lab). Do not reuse the MySQL root password or port 3306 here.

Teacher walkthrough: server password ≠ pgAdmin master

All figures illustrative.

Step 1 — Install PostgreSQL server; set and store the superuser password.

Step 2 — Prove server running before opening pgAdmin.

Step 3 — Register server in pgAdmin with host/port/user and the server password (not the pgAdmin UI master password, if prompted separately).

Step 4 — Create lab DB; optional psql connect as in Tutorial 7.

Failure example: mixed the two passwords

Symptom: pgAdmin opens but server registration fails repeatedly.

Diagnosis: Typed pgAdmin master password into server auth (or vice versa).

Correction: Re-read "Two passwords" section; reset only with official docs if locked out.

Validation: Browser/tree shows databases; SELECT 1 works in Query Tool or psql.

Guided practice

Which password does psql -U postgres need?

Complete solution: The PostgreSQL role/server password — not the optional pgAdmin master password.

Independent practice

Acceptance criteria:

Common mistakes

  1. Guessing http://localhost URLs instead of opening pgAdmin desktop.
  2. Creating databases before the server is running.
  3. Sharing superuser password in chat logs.

Checklist

  • [ ] pg_isready says accepting connections (or the Windows service is Running)
  • [ ] I can tell the postgres password from the pgAdmin master password
  • [ ] pgAdmin shows Local PostgreSQL connected
  • [ ] Database learnings_pg exists and schema public was already there
  • [ ] Scratch table lab_notes exists
  • [ ] I connected with psql and \conninfo matched host, port, user, and database
  • [ ] I can write the same four facts as a psycopg2.connect(...) call or a postgresql+psycopg2:// URL
  • [ ] Ready for psql — I will not stay in the GUI for the rest of the track