In this article we will see how to connect to PostgreSQL from Python Script and perform queries.
PostgreSQL is an open source object-relational database management system. PostgreSQL is ACID-compliant and is transactional. It has triggers, foreign keys and supports functions and stored procedures.
PostgreSQL is used by giants like Uber, Apple, Netflix and Instagram.
Create a virtual environment using python 3 and activate it. Install below packages.
psycopg2==2.7.3.2
Install the PostgreSQL database and utilities using below commands.
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
By default, PostgreSQL sets up the user and database “postgres” upon a new installation. We need to switch to this user to use postgres database.
sudo -su postgres
Now go to the Postgres prompt by typing psql
on terminal.
We are using version 10.3
.
If you get any error in connecting to database, make sure PostgreSQL is running. Check the status using below command.
$ systemctl status postgresql
You can check for errors in logs using below command.
$ tail -f /var/log/postgresql
Before creating a new database, lets list all the databases. Use \l
or \list
for the same.
To create database, exit the psql terminal by typing \q
and use command createdb testdb
.
postgres@brahma:~$ createdb testdb
postgres@brahma:~$ psql
psql (10.3 (Ubuntu 10.3-1.pgdg16.04+1))
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
---------------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | en_IN | en_IN |
rana_test | postgres | UTF8 | en_IN | en_IN |
template0 | postgres | UTF8 | en_IN | en_IN | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_IN | en_IN | =c/postgres +
| | | | | postgres=CTc/postgres
testdb | postgres | UTF8 | en_IN | en_IN |
(5 rows)
postgres=# \c testdb
You are now connected to database "testdb" as user "postgres".
testdb=#
To connect to another database, use command \c
or \connect
and database name. \c testdb
in this case.
Most the query sytanx in PostgreSQL are same as MySQL.
create table users (
id serial PRIMARY KEY,
username varchar (20) NOT NULL,
age smallint NOT NULL,
location varchar (50) NOT NULL
);
Copy paste the above sytax in terminal and new table will be created. You can list the tables by typing \d
.
testdb=# create table users (
testdb(# username varchar (20) NOT NULL,
testdb(# age smallint NOT NULL,
testdb(# location varchar (50) NOT NULL
testdb(# );
CREATE TABLE
testdb=# \d
List of relations
Schema | Name | Type | Owner
--------+-------+-------+----------
public | users | table | postgres
(1 row)
testdb=#
You can learn more about querying from psql terminal by visitng official site. Lets go to Python code.
We installed the psycopg package in virtual environment. Use below code in Python Script to connect to database.
import psycopg2
# this function will return the connection object
def connect():
conn = None
try:
conn = psycopg2.connect(host="localhost", user="postgres", password="root", database="testdb")
except Exception as e:
print(repr(e))
return conn
First get the connection and cursor and then create query. Once query is executed, commit using connection and close the cursor and connection.
conn = connect()
cur = conn.cursor()
last_insert_id = None
# inserting data in users table
sql_query = "insert into users (username, age, location) values (%s, %s, %s) returning id;"
sql_data = (
"Ajay",
"25",
"New York"
)
cur.execute(sql_query, sql_data)
last_insert_id = cur.fetchone()[0]
print("Last Insert ID " + str(last_insert_id))
conn.commit()
cur.close()
conn.close()
return last_insert_id
We are Inserting data in table and returning the primary key id
which is the serial key.
Select query for PostgreSQL is same as MySQL.
conn = connect()
cur = conn.cursor()
sql_query = "select username, age, location from users where location = %s;"
sql_data = ("Delhi")
cur.execute(sql_query, sql_data)
results = cur.fetchall()
return results
conn = connect()
cursor = conn.cursor()
sql_query = "update users set location = %s where username = %s;"
sql_data = ("Mumbai", "Ajay")
cursor.execute(sql_query, sql_data)
cursor.close()
conn.close()
return True
To exit the terminal use \q
command.
If you are facing any issue, feel free to comment.
#python #postgresql #web-development