PostgreSQL Guide - ganmath/learners GitHub Wiki
Technical documentation covering the steps and commands to install, configure, and troubleshoot PostgreSQL on a Windows system, using Chocolatey as the installation method and focusing on common issues such as configuration and service management.
Installation of PostgreSQL via Chocolatey
-
Open PowerShell as Administrator:
- Press
Win + Xand choose “Windows PowerShell (Admin)”.
- Press
-
Install PostgreSQL using Chocolatey:
choco install postgresql --params '/Password:<YourPassword>'Replace
<YourPassword>with a strong password for the PostgreSQLpostgresuser. -
Verify Installation:
choco list --local-only
Post-Installation Configuration
-
Add PostgreSQL to System PATH (optional but recommended for easy command-line access):
- Open System Properties (type
sysdm.cplin Run dialog). - Navigate to the “Advanced” tab and click “Environment Variables”.
- In the “System variables” section, find and select “Path”, then click “Edit”.
- Click “New” and add the path
C:\Program Files\PostgreSQL\16\bin. - Click “OK” to close all dialogs.
- Open System Properties (type
-
Start PostgreSQL Service:
- Press
Win + R, typeservices.msc, and press Enter. - Locate the PostgreSQL service (e.g.,
postgresql-x64-16), right-click it, and select “Start”.
- Press
-
Set the
PGDATAEnvironment Variable:[System.Environment]::SetEnvironmentVariable("PGDATA", "C:\Program Files\PostgreSQL\16\data", "Machine")
Initialization of Database Cluster
- Initialize the PostgreSQL Data Directory:
cd "C:\Program Files\PostgreSQL\16\bin" initdb -D "C:\Program Files\PostgreSQL\16\data"
Starting PostgreSQL Manually (Not Recommended for Production)
- Open a Command Prompt (not as Administrator):
- Navigate to the bin directory:
cd "C:\Program Files\PostgreSQL\16\bin" - Start PostgreSQL:
postgres -D "C:\Program Files\PostgreSQL\16\data"
- Navigate to the bin directory:
Verifying PostgreSQL Configuration Files
-
Locate and Inspect Configuration Files:
postgresql.confandpg_hba.confare located inC:\Program Files\PostgreSQL\16\data.
-
Edit Configuration Files if Necessary:
- Use a text editor to adjust settings such as
listen_addressesand connection permissions inpg_hba.conf.
- Use a text editor to adjust settings such as
Troubleshooting Common Errors
-
Service Not Visible in Windows Services:
- Re-register the service:
pg_ctl register -N "postgresql" -D "C:\Program Files\PostgreSQL\16\data" -U "NT AUTHORITY\NetworkService"
- Re-register the service:
-
Configuration File Not Found Error:
- Ensure
PGDATAis set correctly and points to a directory initialized withinitdb. - Verify that the configuration files exist in the data directory.
- Ensure
-
Permission Errors When Starting PostgreSQL:
- Ensure PostgreSQL is not run as an administrator for regular operations.
General Maintenance and Operation
-
Starting and Stopping PostgreSQL:
pg_ctl start pg_ctl stop -
Accessing PostgreSQL via Command Line:
psql -U postgres
Reinstallation or Reset
- If troubleshooting fails:
choco uninstall postgresql choco install postgresql --params '/Password:<YourNewPassword>'
This documentation provides a comprehensive guide to managing a PostgreSQL installation on Windows, addressing installation, configuration, daily operation, and common troubleshooting scenarios.