PostgreSQL 101
π How to Install and Run PostgreSQL (Windows)
Step 1: Download PostgreSQL
Go to the official website:
π https://www.postgresql.org/download/windows/
Click Download the installer → it will redirect to EnterpriseDB.
Choose:
Latest stable version (e.g., PostgreSQL 16)
Windows x86-64
Download the .exe installer.
Step 2: Install PostgreSQL
Run the installer and follow this setup:
-
Installation Directory
Leave default (recommended). -
Components
✔ PostgreSQL Server
✔ pgAdmin 4
✔ Command Line Tools -
Data Directory
Leave default. -
Set Password for Superuser (postgres)
⚠️ VERY IMPORTANT
Remember this password. You will use it to login. -
Port Number
Default is:5432Leave it unless you already have something using that port.
-
Click Next → Install → Finish.
To add psql to Windows Environment Variables
✅ Step 1: Find PostgreSQL bin Folder
Go to where PostgreSQL is installed.
Usually it is:
C:\Program Files\PostgreSQL\16\bin
(Replace 16 with your version.)
Inside that folder, you should see:
psql.exe
pg_dump.exe
pg_restore.exe
Copy the full path of that bin folder.
✅ Step 2: Add to Environment Variable (PATH)
-
Press Windows Key
-
Search:
Environment Variables -
Click
Edit the system environment variables -
Click Environment Variables
-
Under System variables
-
Find Path
-
Click Edit
-
-
Click New
-
Paste:
C:\Program Files\PostgreSQL\16\bin
-
Click OK → OK → OK
✅ Step 3: Restart Command Prompt
⚠️ Important: Close ALL Command Prompts.
Open a new one.
Type:
psql --version
If successful, you’ll see:
psql (PostgreSQL) 16.x
π Done.
Step 3: Check if PostgreSQL is Running
After installation:
Option A: Check via Services
Press:
Windows + R
Type:
services.msc
Look for:
postgresql-x64-16 (or your version)
Status should be:
Running
π How to Run PostgreSQL
There are 2 main ways:
Method 1: Using pgAdmin (GUI – Easy Way)
-
Open pgAdmin 4
-
It will ask for master password
-
Expand:
Servers → PostgreSQL 16 → Databases -
Right click Databases
-
Click Create → Database
-
Give name → Save
Now your database is ready π
To run SQL:
-
Click your database
-
Click Query Tool
-
Write:
SELECT version();
Click ▶ Run.
Method 2: Using Command Line (psql)
Open:
SQL Shell (psql)
It will ask:
Server [localhost]:
Database [postgres]:
Port [5432]:
Username [postgres]:
Password:
Press Enter for defaults.
Enter your password.
If successful, you’ll see:
postgres=#
Now you can run:
CREATE DATABASE testdb;
Connect to it:
\c testdb
Create table:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100)
);
Insert data:
INSERT INTO users (name) VALUES ('Nurah');
Check:
SELECT * FROM users;
π If PostgreSQL Doesn’t Start
Check:
-
Port 5432 already used?
-
Windows Firewall blocking?
-
Service not running?
You can restart service from:
services.msc
Ulasan