Step 1: Install PostgreSQL on Windows
Download the installer from:
https://www.postgresql.org/download/windows
Run the installer and:
- Choose a password for the postgres user (save it).
- Install pgAdmin if you want a GUI.
- Leave default port 5432.
- Complete the installation.
During the installation of PostgreSQL on Windows, you are asked to select components like:
- PostgreSQL Server
- pgAdmin 4
- Stack Builder
- Command Line Tools
1. PostgreSQL Server (Essential)
- The actual database server that stores and manages your databases.
- It’s the core service—without this, PostgreSQL won’t run.
- ✅ You must select this.
✅ 2. pgAdmin 4 (Optional but Recommended)
- A web-based GUI (Graphical User Interface) for managing PostgreSQL databases.
- Why you might want it:
- Makes it easier to create databases, users, and run queries without using the command line.
- Very useful if you’re new to PostgreSQL.
- Optional, but recommended for beginners.
❌ 3. Stack Builder (Not required ❌)
- A tool to download and install additional software or drivers, such as:
- PostgreSQL extensions
- Foreign data wrappers
- ODBC/JDBC drivers
- Why you might want it: If you need extras like PostGIS (for geospatial support), you can install them through Stack Builder.
- ❌ Not necessary for most Django projects. You can skip it.
✅ 4. Command Line Tools (Essential ✅)
- Includes tools like:
psql: A command-line interface for interacting with PostgreSQL.- Other utilities for database backup/restore (
pg_dump,pg_restore, etc.)
- Useful for advanced operations, scripting, and debugging.
- ✅ Yes, recommended and often needed.
Step 2: Add PostgreSQL to System PATH
- Go to:
- Start → Environment Variables → System Variables → Path
- Add the path to PostgreSQL binaries (e.g.,
C:\Program Files\PostgreSQL\16\bin) - Click OK and restart your terminal/command prompt
- Run Command prompt as Administrator user
Step 3: Create a Database and User in pgAdmin
- Open pgAdmin.
- Login with the postgres user and password.
- Right-click Databases → Create → Database
- Name:
myprojectdb
- Name:
- Right-click Login/Group Roles → Create → Role
- Name:
myprojectuser - Go to Definition tab → Set password.
- Go to Privileges tab → Enable all privileges.
- Name:
- Go back to the database → right-click → Properties → Security
- Add your
myprojectuseras the owner (optional).
- Add your
Step 4: Install Python PostgreSQL Adapter
In your virtual environment or project directory:
pip install psycopg2-binary
Step 5: Configure Django settings.py
In your Django project’s settings.py, update the DATABASES section:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'your_password_here',
'HOST': 'localhost',
'PORT': '5432',
}
}
Step 6: Run Migrations
Open Command Prompt or PowerShell in your Django project folder:
python manage.py makemigrations
python manage.py migrate
Step 7: Create Superuser
python manage.py createsuperuser
PostgreSQL must be installed on your system.
The PostgreSQL bin directory should be added to the Windows PATH.
- Usually:
C:\Program Files\PostgreSQL\<version>\bin - Confirm by running
psql --versionin Command Prompt.
To Create a PostgreSQL Database via Command Prompt
Step 1: Open Command Prompt (as Administrator)
- Press
Win + R, typecmd, and press Enter. - Or search “Command Prompt” and right-click → Run as Administrator.
Step 2: Switch to PostgreSQL User (Optional)
If postgres user was created with a password during installation, you can connect directly with:
psql -U postgres
If psql is not recognized, check if the PostgreSQL bin path is in your system’s PATH variable.
Step 3: Enter Password (If prompted)
- When prompted for a password, enter the one you set during installation.
Step 4: Create a New Database and User
Now you’re inside the PostgreSQL shell. Run the following commands:
-- Create database
CREATE DATABASE myprojectdb;
-- Create user with password
CREATE USER myprojectuser WITH PASSWORD 'mypassword';
-- Grant privileges
GRANT ALL PRIVILEGES ON DATABASE myprojectdb TO myprojectuser;
To exit the psql shell:
\q
How to Add PostgreSQL bin Directory to Windows PATH
🔍 Step 1: Find the PostgreSQL bin Folder
By default, it is usually located at:
C:\Program Files\PostgreSQL\<version>\bin
Example (for version 17):
C:\Program Files\PostgreSQL\16\bin
Replace 17 with your actual installed version.
Step 2: Add to System PATH
For Windows 10 / 11:
- Press Windows + S and search for Environment Variables.
- Click on “Edit the system environment variables”.
- In the System Properties window, click on Environment Variables (bottom right).
- Under System variables, find and select the variable named
Path, then click Edit. - Click New and paste your PostgreSQL
binpath, e.g.:
C:\Program Files\PostgreSQL\16\bin
6. Click OK on all windows to apply the changes.
tep 3: Restart Command Prompt
- Close any open Command Prompt windows.
- Open a new Command Prompt.
- Type
psql --versionto verify it’s working.
psql (PostgreSQL) 17.1
How to Set or Reset the Password for postgres User
Use the Query Tool in pgAdmin
Steps:
- Open pgAdmin 4.
- Connect to your server.
- From the left pane, navigate to:
Servers > PostgreSQL <version> > Databases > postgres
Right-click on postgres (database) and click Query Tool.
In the query window, run:
ALTER USER postgres WITH PASSWORD 'YourNewSecurePassword';
Click the Execute/Run button (lightning icon