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:

  1. Installation Directory
    Leave default (recommended).

  2. Components
    ✔ PostgreSQL Server
    ✔ pgAdmin 4
    ✔ Command Line Tools

  3. Data Directory
    Leave default.

  4. Set Password for Superuser (postgres)
    ⚠️ VERY IMPORTANT
    Remember this password. You will use it to login.

  5. Port Number
    Default is:

    5432

    Leave it unless you already have something using that port.

  6. 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)

  1. Press Windows Key

  2. Search:

    Environment Variables
  3. Click
    Edit the system environment variables

  4. Click Environment Variables

  5. Under System variables

    • Find Path

    • Click Edit

  6. Click New

  7. Paste:

C:\Program Files\PostgreSQL\16\bin
  1. 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)

  1. Open pgAdmin 4

  2. It will ask for master password

  3. Expand:

    Servers → PostgreSQL 16 → Databases
  4. Right click Databases

  5. Click Create → Database

  6. 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


πŸš€ Now You Can Import Dump

Example:

psql -U postgres -d tacdb -f "C:\Users\Nurah\Downloads\database_dump.sql"




Ulasan

Catatan popular daripada blog ini

SISTEM PENGOPERASIAN KOMPUTER (OS)

APA ITU ASCII (AMERICAN STANDARD CODE FOR INFORMATION INTERCHANGE) ?

APA ITU STRUCTURED QUERY LANGUAGE (SQL)