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
| Resource | Link |
|---|---|
| Previous | MySQL install |
| Next | Power BI install |
| Time | ~25 minutes |
| What you need | A 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
| Password | When you set it | What it unlocks |
|---|---|---|
| postgres superuser | During PostgreSQL installation | The database. This is what the Connection tab wants. |
| pgAdmin master password | First time you open pgAdmin | pgAdmin’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
- Download the PostgreSQL installer from postgresql.org/download (Windows: the EDB installer).
- Include PostgreSQL Server, pgAdmin 4, and Command Line Tools.
- 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.
- 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 5432You 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:
- Name:
Local PostgreSQL(any label you will recognise — this name is only inside pgAdmin)
Connection tab:
| Field | Value |
|---|---|
| Host name/address | localhost (or 127.0.0.1) |
| Port | 5432 |
| Maintenance database | postgres |
| Username | postgres |
| Password | The 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.
- Database:
learnings_pg(this track reuses that name in pg1) - Owner: leave
postgresfor this lab
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:
| Name | Type | Notes |
|---|---|---|
id | serial | Mark as primary key. serial is the older auto-number spelling; pg2 shows the newer IDENTITY form. |
title | varchar | Length 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| Flag | What the GUI called it |
|---|---|
-h localhost | Host name/address |
-p 5432 | Port |
-U postgres | Username |
-d learnings_pg | The 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-binarypsycopg2-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 / action | What it does |
|---|---|
pg_isready -h localhost -p 5432 | Checks that the server is accepting connections — do this before pgAdmin |
Services → postgresql… Running | Windows check that the engine service is up |
| Register Server → Connection | Points pgAdmin at an engine that already exists |
CREATE DATABASE learnings_pg; | Creates the lab database (from the postgres database) |
public schema | Already 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_pg | The Connection tab as a command. Password prompt is the postgres user |
\conninfo | Prints 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_pg | SQLAlchemy URL. Name the driver |
Practice
Do this now:
- Run
pg_isready -h localhost -p 5432and read the line it prints. - Register
Local PostgreSQLin pgAdmin with hostlocalhost, port5432, maintenance databasepostgres, userpostgres. - Create database
learnings_pg. - Confirm schema
publicalready exists, then createlab_notes(GUI or SQL). - Quit pgAdmin. Connect with
psql -h localhost -p 5432 -U postgres -d learnings_pg, run\conninfo, thenSELECT * FROM lab_notes;. An empty result is success. - Optional: the same
SELECTfrompsycopg2or SQLAlchemy, usingPGPASSWORDrather 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:
- Server running; version noted.
- Lab database created.
- GUI and one CLI connect proven (or honest blocker).
- Passwords stored outside git.
Common mistakes
- Guessing http://localhost URLs instead of opening pgAdmin desktop.
- Creating databases before the server is running.
- Sharing superuser password in chat logs.
Checklist
- [ ]
pg_isreadysaysaccepting connections(or the Windows service is Running) - [ ] I can tell the postgres password from the pgAdmin master password
- [ ] pgAdmin shows
Local PostgreSQLconnected - [ ] Database
learnings_pgexists and schemapublicwas already there - [ ] Scratch table
lab_notesexists - [ ] I connected with
psqland\conninfomatched host, port, user, and database - [ ] I can write the same four facts as a
psycopg2.connect(...)call or apostgresql+psycopg2://URL - [ ] Ready for psql — I will not stay in the GUI for the rest of the track