PostgreSQL Windows
Bootstrapping PostgreSQL On Your Windows Machine
Section titled “Bootstrapping PostgreSQL On Your Windows Machine”Part Zero: Documentation
Section titled “Part Zero: Documentation”PostgreSQL Docs
psycopg3 Docs
Part One: Download and Installation
Section titled “Part One: Download and Installation”Download the PostgreSQL installer
Run the installer and install PostgreSQL Server and Command Line Tools (psql)
Part Two: Setting Up the Database
Section titled “Part Two: Setting Up the Database”-
Run PowerShell as admin
-
Run
psql -U postgresto open the postgres shell If that doesn’t work, try& "C:\Program Files\PostgreSQL\18\bin\psql.exe" -U postgres(or restart your computer and try again) -
Run
CREATE DATABASE db_name;to create a new database calleddb_name -
Run
\c db_nameto navigate to said database 5. To import a SQL database, run\i 'C:/Users/monalisa/Documents/Path/To/Database/File.db.sql'
Part Three: Testing the Database
Section titled “Part Three: Testing the Database”Run \dt to query all the tables in your database, and SQL commands to query your database from the postgres shell
Part Four: Giving Your Account Permissions
Section titled “Part Four: Giving Your Account Permissions”-
Run
Terminal window CREATE USER username WITH PASSWORD 'password';where
usernamematches your system username. CheckC:/Usersto view the users. Your account will be listed there. -
Run
Terminal window ALTER USER username WITH SUPERUSER;to give the newly created account
SUPERUSERstatus -
Restart postgres or restart your computer
-
To update your password, run
Terminal window ALTER USER username WITH PASSWORD 'new_password'; -
To explicitly grant connection access, run
GRANT CONNECT ON DATABASE db_name TO username;
Part Five: Python Integration
Section titled “Part Five: Python Integration”install psycopg and import psycopg into your python project
Run the program. If psycopg isn’t finding PostgreSQL, try
-
Press Win + r
-
Type
sysdm.cpland hit OK -
Navigate to
Advanced > Environment Variables -
Navigate to
System Variables > Path > Edit > New > Add, and pasteC:\Program Files\PostgreSQL\18\bin. If you’re using a different version of PostgreSQL, make sure that the path is accurate, i.e. a user using PostgreSQL 17 will pasteC:\Program Files\PostgreSQL\17\bin. If you installed PostgreSQL in a different location other thanC:\Program Files, use that path instead -
Restart postgres or restart your computer
Part 6: Retrieving Dumps and Logs
Section titled “Part 6: Retrieving Dumps and Logs”-
Open
postgresql.conf -
Uncomment and update the following line:
log_statement = 'all'. This will make postgres log all queries, including ones from external connections. -
Run
Powershellas admin -
Run
cd "C:/Users/monalisa/Documents/Path/To/Dump/Location"to navigate to the location you desire to save the.dumpfiles -
Run
pg_dump -U postgres -d db_name --params > filename.dumpto save a.dumpfile with the specifications outlined by--params -
You can find the
pg_dumpdocumentation here -
Find the log file using
SELECT pg_current_logfile();when in the postgres shell