How to connect Postgres SQL using Python

How to connect Postgres SQL using Python

Step 1: Confirm Postgres SQL is running fine

C:\Users\mudhalvan>psql -U mudhalvan testdb01
psql (11.1)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page “Notes for Windows users” for details.
Type “help” for help.

testdb01=# \d emp
Table “public.emp”
Column | Type | Collation | Nullable | Default
——–+—————+———–+———-+———
eno | integer | | |
ename | character(10) | | |

testdb01=# select * from emp;
eno | ename
—–+————
1 | Mudhalvan
2 | Aarthi
(2 rows)

Step 2: Confirm Python is installed and it has Library (psycopg2) to connect Postgres

If not install it

C:\WINDOWS\system32>pip install psycopg2
Collecting psycopg2
Installing collected packages: psycopg2
Successfully installed psycopg2-2.7.7

Step 3: Connect to database.

C:\WINDOWS\system32>python

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.

import psycopg2

conn=psycopg2.connect(“dbname=testdb01 user=mudhalvan password=xxxx”)
cur=conn.cursor()
cur.execute(“select * from emp”)
for line in cur:
… print(line)

(1, ‘Mudhalvan ‘)
(2, ‘Aarthi ‘)
cur.close()
conn.close()

Now you can write a simple .py script with the above lines and confirm it.

How to Install PgSQL (Postgres SQL) in windows

Step 1: Download Executable files
Download respective binaries for 32 Bit or 64 bit of latest available version

https://www.postgresql.org/

Step 2: Install the binaries

Once binary is downloaded and installed in the computer with respective username of windows user. 

Installation Directory: D:\PgSQL
Server Installation Directory: D:\PgSQL
Data Directory: D:\PgSQL\data
Database Port: 5432
Database Superuser: postgres
Operating System Account: NT AUTHORITY\NetworkService
Database Service: postgresql-x64-11
Command Line Tools Installation Directory: D:\PgSQL
pgAdmin4 Installation Directory: D:\PgSQL\pgAdmin 4
Stack Builder Installation Directory: D:\PgSQL

Step 3: Start the Postgres Service

D:\PgSQL>pg_ctl -D d:\pgsql\data initdb
The files belonging to this database system will be owned by user “mudhalvan”.
This user must also own the server process.

The database cluster will be initialized with locale “English_United States.1252”.
The default database encoding has accordingly been set to “WIN1252”.
The default text search configuration will be set to “english”.

Data page checksums are disabled.

fixing permissions on existing directory d:/pgsql/data … ok
creating subdirectories … ok
selecting default max_connections … 100
selecting default shared_buffers … 128MB
selecting dynamic shared memory implementation … windows
creating configuration files … ok
running bootstrap script … ok
performing post-bootstrap initialization … ok
syncing data to disk … ok

WARNING: enabling “trust” authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
–auth-local and –auth-host, the next time you run initdb.

Success. You can now start the database server using:

d:/pgsql/bin/pg_ctl -D d:/pgsql/data -l logfile start

D:\PgSQL>

Step 4: Start the Database server

D:\PgSQL>pg_ctl -D d:/pgsql/data -l logfile start
waiting for server to start…. done
server started

D:\PgSQL>

Step 5: Create Database

D:\PgSQL\bin>createdb -h localhost -U mudhalvan testdb01

Step 6: Connect to the database

D:\PgSQL\bin>psql -U mudhalvan testdb01 -h localhost
psql (11.1)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page “Notes for Windows users” for details.
Type “help” for help.

To List the databases

testdb01=# \l

Step 7: Create Table

testdb01=# create table emp (eno integer,ename char(10));
CREATE TABLE

Step 8: Describe the Table

testdb01=# \d emp
Table “public.emp”
Column | Type | Collation | Nullable | Default
——–+—————+———–+———-+———
eno | integer | | |
ename | character(10) | | |

Step 9: Insert the record into Table
testdb01=# insert into emp values(1,’Mudhalvan’);
INSERT 0 1
testdb01=# select * from emp;
eno | ename
—–+————
1 | Mudhalvan
(1 row)