The short version
- One installer. The EDB interactive installer is the Windows route to PostgreSQL 18.
- Four things in the box. Server, pgAdmin 4, command line tools, optional Stack Builder.
- Password and port. Set a password for the
postgressuperuser; keep port 5432. - Service and data. You get
postgresql-x64-18andC:\Program Files\PostgreSQL\18\data. - PATH is your job. Add the
binfolder yourself, thenpsqlworks in a new terminal. - Verify twice.
Get-Service postgresql*shows Running, thenpsql -U postgreslets you in.
Before you start
You installed MySQL in the previous chapter; a real PostgreSQL server is what makes MySQL vs PostgreSQL useful. Install PostgreSQL 18, minor release 18.6. PostgreSQL 19 is still a beta, so it is a preview for testing and never for work.
The Windows route is the interactive installer built by EDB, about 394 MB, named postgresql-18.6-4-windows-x64.exe. It asks for a directory, a password, a port and a locale, then runs initdb and registers a Windows service. MySQL needs a separate configurator; PostgreSQL does it all in one wizard.
Windows
Windows 11, 64-bit (also Server 2022 and 2025)
Rights
Run the installer as Administrator
Names
ASCII-only Windows user and computer names
Data directory
Must be empty, or the install fails
Install step by step
- Download the installer.
Open postgresql.org/download/windows and click "Download the installer". On EDB's page, click the Windows x86-64 icon in the 18.6 row.
- Run it as Administrator.
Right-click the file, choose "Run as administrator", click Next.
- Installation directory.
Keep
C:\Program Files\PostgreSQL\18. The programs land in itsbinsubfolder. - Select components.
Keep PostgreSQL Server, pgAdmin 4 and Command Line Tools ticked. Untick Stack Builder.
- Data directory.
Keep
C:\Program Files\PostgreSQL\18\data. Write the path down; configuration files and logs live there. - Password for the superuser.
This is the password of the database role
postgres. Put it in your password manager now. - Port.
Keep 5432. If the installer proposes another number, something already listens on 5432, so note whatever it picks.
- Locale.
Keep "[Default locale]". It decides how text sorts, and it is permanent for the cluster, so every database you create later inherits it.
- Summary, then install.
Click Next twice and wait. The installer runs
initdb(UTF8 encoding,scram-sha-256passwords, data checksums) and registers the servicepostgresql-x64-18, running asNT AUTHORITY\NetworkService. - Finish.
Untick "Launch Stack Builder at exit" and click Finish.
- Check the service.
Open PowerShell.
PowerShell # Is the server running? The wildcard saves you the exact name. Get-Service postgresql*Status Name DisplayName Running postgresql-x64-18 postgresql-x64-18 - PostgreSQL Server 18 - Add the tools to PATH.
The installer never touches PATH, so
psqlis unknown to every terminal until you add thebinfolder.PowerShell # This window only $env:Path += ';C:\Program Files\PostgreSQL\18\bin' # Permanently, for your user account (open a new terminal afterwards) [Environment]::SetEnvironmentVariable('Path', [Environment]::GetEnvironmentVariable('Path', 'User') + ';C:\Program Files\PostgreSQL\18\bin', 'User') - Connect with psql.
Open a new terminal. There is no
postgresoperating-system user on Windows, sosudo -u postgres psqlnever applies; you name the role with-U. Runchcp 1252first so accented text displays correctly.PowerShell chcp 1252 psql -U postgres # Password for user postgres: ******** # postgres=# - Load the shop dataset.
Run the shared script from the course folder. It recreates
shopand the tables every chapter uses.PowerShell cd C:\path\to\the\course psql -U postgres -f assets/sample-shop.postgres.sql
Remember
Write down three things at install time: the postgres password, the port, and the data directory path.
Your first ten minutes
Create a role and a database
Working as postgres all day is like working as root, so create a login role for yourself. At work the same statement creates an application's account, usually without CREATEDB. More in Users, roles and security.
-- A role is users and groups in one concept; LOGIN makes it a MySQL-style user
CREATE ROLE yourname WITH LOGIN PASSWORD 'pick-a-real-one' CREATEDB;
-- One cluster holds many databases; OWNER makes a role responsible for one
CREATE DATABASE playground OWNER yourname;
Then connect: psql -U yourname -d playground. Without -d, psql assumes a database named after the role and fails. To stop retyping passwords, put one line per server in %APPDATA%\postgresql\pgpass.conf: hostname:port:database:username:password. Treat that file like a password.
Find your way around psql
A backslash command is handled by the client and takes no semicolon. Anything else is SQL.
| psql | What it does | MySQL |
|---|---|---|
\l | List databases in this cluster | SHOW DATABASES |
\c shop | Connect to another database (a new connection) | USE shop |
\dt | List tables in the current database | SHOW TABLES |
\d orders | Columns, indexes, constraints, references | SHOW CREATE TABLE |
\du | List roles | SELECT user FROM mysql.user |
\q | Quit. \? lists every backslash command | quit |
The list includes template0 and template1. Templates are what CREATE DATABASE copies, which is why the install-time locale sticks.
| Name | Owner | Encoding | Collate |
|---|---|---|---|
| playground | yourname | UTF8 | English_United States.1252 |
| postgres | postgres | UTF8 | English_United States.1252 |
| shop | postgres | UTF8 | English_United States.1252 |
| template0 | postgres | UTF8 | English_United States.1252 |
| template1 | postgres | UTF8 | English_United States.1252 |
The two files a DBA edits
Both live in the data directory. postgresql.conf is the settings file, the counterpart of my.ini: port, listen_addresses, memory, logging. pg_hba.conf is the gatekeeper: one line per rule, saying which address may reach which database as which role, with which method. If you lose them, ask the server.
SHOW data_directory; -- C:/Program Files/PostgreSQL/18/data
SHOW config_file; -- .../data/postgresql.conf
SHOW hba_file; -- .../data/pg_hba.conf
SELECT pg_reload_conf(); -- re-read both files without a restart
On Windows there are no Unix sockets, so psql always uses TCP to localhost and only these two lines matter.
# pg_hba.conf as the installer wrote it (comments trimmed)
# TYPE DATABASE USER ADDRESS METHOD
host all all 127.0.0.1/32 scram-sha-256
host all all ::1/128 scram-sha-256
Changes to pg_hba.conf take effect on reload. Changes to port or listen_addresses need a full restart. Tuning both files is covered in A DBA's working week.
For you as a DBA
When a developer says "PostgreSQL rejects my connection", ask two questions: is there a pg_hba.conf line matching their address, database and role, and does listen_addresses include their interface?
Start and stop the service
The service is automatic, so it returns after every reboot. Control it from an elevated PowerShell; keep pg_ctl for status and reload.
# From an elevated PowerShell (Run as administrator)
Stop-Service postgresql-x64-18
Start-Service postgresql-x64-18
Restart-Service postgresql-x64-18
# Reload configuration files without dropping connections
pg_ctl reload -D "C:\Program Files\PostgreSQL\18\data"
pgAdmin's first launch
pgAdmin 4 is in the Start menu under PostgreSQL 18. Its master password only encrypts the server passwords pgAdmin saves; it has nothing to do with the database. Expand Servers, click "PostgreSQL 18", enter the postgres password, then open the Query Tool on shop. Clients and tools compares the clients.
Ask the AI
Give the assistant the version, the platform and the evidence: "PostgreSQL 18.6 on Windows 11, EDB installer. The postgresql-x64-18 service stops right after I start it. Here are the last 30 lines of the newest log file. What should I change?"
Other ways to install
winget and Chocolatey run the same EDB installer with fewer clicks, and end with the same service name and data directory. In winget's unattended mode the superuser password defaults to postgres, so change it immediately.
winget install -e --id PostgreSQL.PostgreSQL.18
choco install postgresql18 --params "/Password:YourPassword /Port:5432"
When it goes wrong
Every one of these has a boring cause.
| Symptom | Likely cause | What to do |
|---|---|---|
Forgot the postgres password | It is stored as a hash and cannot be read back | Follow the reset procedure below. Do not reinstall. |
| "psql is not recognized" | bin not on PATH, or the terminal predates the change | Open a new terminal. The full path works meanwhile: & "C:\Program Files\PostgreSQL\18\bin\psql.exe" -U postgres |
| Service will not start or stops at once | Port 5432 taken; NetworkService cannot write the data directory; antivirus; stale postmaster.pid | Read the newest file in data\log and Event Viewer. Check the port with netstat -ano | findstr 5432. Exclude the data directory from antivirus. Delete postmaster.pid only when no postgres process runs. |
| Cannot connect from another machine | Listening on localhost only; no matching pg_hba.conf line; firewall | Set listen_addresses = '*' (restart), add host all all 192.168.1.0/24 scram-sha-256 (reload), allow inbound TCP 5432 in Windows Defender Firewall. |
| "password authentication failed for user" | Wrong password or role, or a driver too old for scram-sha-256 | Retype carefully; check the role with \du; update the driver. Do not downgrade to md5. |
| "database yourname does not exist" | psql defaults the database name to the role name | Add -d postgres or -d shop. |
| "Console code page (437) differs from Windows code page (1252)" | Plain console encoding | Run chcp 1252 before psql in that window. |
Resetting a forgotten postgres password
Trust local connections for a minute. Back up pg_hba.conf first, then edit it as Administrator.
- Open trust.
In
C:\Program Files\PostgreSQL\18\data\pg_hba.conf, change the METHOD on the127.0.0.1/32and::1/128lines totrust. - Reload.
Restart-Service postgresql-x64-18from an elevated PowerShell. - Set a new password.
psql -U postgresnow lets you in without a password.Illustration · PostgreSQL ALTER USER postgres WITH PASSWORD 'a-new-strong-one'; - Close the door.
Put
scram-sha-256back and reload. Test withpsql -U postgres; it must ask for the password.
Careful
Never leave trust in pg_hba.conf, and never combine it with listen_addresses = '*'. That is an open superuser account for anyone who can reach the port.
Uninstall cleanly
Settings, Apps, Installed apps, PostgreSQL 18, Uninstall. The uninstaller leaves the data directory behind, so delete C:\Program Files\PostgreSQL\18\data yourself for a clean slate; a reinstall into a non-empty path fails. Remove the bin entry from your PATH, and take a pg_dumpall backup first if the server holds anything you care about (Backup, restore and replication).
Where to go deeper
- EDB: Installing PostgreSQL on Windows
The wizard, screen by screen.
- The pg_hba.conf file
Every field and method.
- psql reference
Options and backslash commands.
- Locale support
Why the locale is permanent.