Python PostgreSQL Tutorial Using Psycopg2 (Complete Guide)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9
At a glance
Powered by AI
The key takeaways are how to install and use Psycopg2 to connect to and perform operations on a PostgreSQL database from Python code.

To install Psycopg2, you can use pip install psycopg2 or pip install psycopg2=specific_version from the command line.

To connect to a PostgreSQL database, you need to provide the username, password, hostname, port, and database name when creating a connection object using psycopg2.connect().

5/12/2020 Python PostgreSQL Tutorial Using Psycopg2 [Complete Guide]

 Tutorials

Python PostgreSQL Tutorial Using Psycopg2


PYnative
 Python Exercises(https://pynativ
 Python Quizze
 Code Editor(https://pynative.com/online-pytho
Last updated on April 26, 2020 |   Python Tricks(https://pynative.com/useful-python-tips-and-

Tweet (https://twitter.com/intent/tweet/?text=Python+PostgreSQL+Tutorial+Using+Psycopg2&url=https%3A%2F%2F2.gy-118.workers.dev/%3A443%2Fhttps%2Fpynative.com%2Fpython-postgresql-tutorial%2F&via=PyNative)

F  share (https://facebook.com/sharer/sharer.php?u=https%3A%2F%2F2.gy-118.workers.dev/%3A443%2Fhttps%2Fpynative.com%2Fpython-postgresql-tutorial%2F)

in  share (https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2F2.gy-118.workers.dev/%3A443%2Fhttps%2Fpynative.com%2Fpython-postgresql-tutorial%2F&title=Python+PostgreSQL+Tutorial+Using+Psycopg2)

P  Pin (https://pinterest.com/pin/create/button/?url=https%3A%2F%2F2.gy-118.workers.dev/%3A443%2Fhttps%2Fpynative.com%2Fpython-postgresql-tutorial%2F&media=https://pynative.com/wp-
content/uploads/2018/08/python_postgresql-1.png&description=Python+PostgreSQL+Tutorial+Using+Psycopg2)

This Python PostgreSQL tutorial demonstrates how to develop Python database applications with the PostgreSQL database server. In
Python, we have serval modules available to connect and work with PostgreSQL. the following are the list.

Psycopg2

pg8000

py-postgresql

PyGreSQL

ocpgdb

bpgsql

SQLAlchemy. SQLAlchemy needs any of the above to be installed separately.

Note: Above all interfaces or modules are adhere to Python Database API Specification v2.0 (PEP 249)
(https://www.python.org/dev/peps/pep-0249/). This API has been designed to encourage and maintain the similarity between the Python
modules that are used to access databases. In other words, the syntax, method and the way of access database are the same in all the
modules.

For this tutorial, we stick to the Psycopg2. Why?

Psycopg2 is the most popular python driver for PostgreSQL.

It is required for most Python and Postgres frameworks.

Actively maintained and support the major version of python i.e. Python 3 and Python 2.

It is thread-safe (threads can share the connections). It was designed for heavily multi-threaded applications.

This tutorial mainly focuses on: –

Installing Psycopg2 and use its API to access the PostgreSQL database.

It then takes you through data insertion, data retrieval, data update, and data deletion.

Next, it will cover transaction management, connection pooling, and error-handling techniques to develop robust python programs
with PostgreSQL.

https://pynative.com/python-postgresql-tutorial/ 1/9
5/12/2020 Python PostgreSQL Tutorial Using Psycopg2 [Complete Guide]

Let see each section now.

Install Psycopg2 using the pip command

You need to install Psycopg2 on your machine to use PostgreSQL from Python. This module is available on pypi.org.

Using pip command, you can install Psycopg2 on any operating system including Windows, macOS, Linux, and Unix and Ubuntu. Use the
following pip command to install Psycopg2.

pip install psycopg2

You can also install a speci c version using the following command.

pip install psycopg2=2.7.5

If you are facing pip install error like “connection error:  [SSL: CERTIFICATE_VERIFY_FAILED] certi cate verify failed (_ssl.c:598)”. You can resolve
this error by setting pypi.org and les.pythonhosted.org as trusted hosts. Please try following the pip command to install Psycopg2.

python -m pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org --trusted-host pypi.python.org psycopg2

Verify Psycopg2 installation

You should get the following messages after running the above command.

Collecting psycopg2

Downloading psycopg2-2.7.5

Installing collected packages: psycopg2

Successfully installed psycopg2-2.7.5

Python PostgreSQL Database Connection

In this section, we will learn how to connect to PostgreSQL through python using psycopg2. You need to know the following detail of PostgreSQL
to perform the connection.

Arguments required to connect PostgreSQL database from Python

The username you use to work with PostgreSQL, The default username for the PostgreSQL database is Postgres.

Password – Password is given by the user at the time of installing the PostgreSQL.

https://pynative.com/python-postgresql-tutorial/ 2/9
5/12/2020 Python PostgreSQL Tutorial Using Psycopg2 [Complete Guide]

g y g g Q

Host Name  – is the server name or Ip address on which PostgreSQL is running. if you are running on localhost, then you can use localhost,
or it’s IP i.e., 127.0.0.0

Database Name – Database name to which you want to connect. Here we are using Database named “postgres_db”.

Steps to connect PostgreSQL through python

Use the connect() method of psycopg2 with required arguments to connect PostgreSQL.

Create a cursor object using the connection object returned by the connect method to execute PostgreSQL queries from Python.

Close the Cursor object and PostgreSQL database connection after your work completes.

Catch Exception if any that may occur during this process.

Python Example to Connect PostgreSQL Database

To connect the PostgreSQL database and perform SQL queries you must know the database name you want to connect. If you have not created
any database, I advise you to create one before proceeding further.

import psycopg2
try:
connection = psycopg2.connect(user = "sysadmin",
password = "pynative@#29",
host = "127.0.0.1",
port = "5432",
database = "postgres_db")

cursor = connection.cursor()
# Print PostgreSQL Connection properties
print ( connection.get_dsn_parameters(),"\n")

# Print PostgreSQL version


cursor.execute("SELECT version();")
record = cursor.fetchone()
print("You are connected to - ", record,"\n")

except (Exception, psycopg2.Error) as error :


print ("Error while connecting to PostgreSQL", error)
finally:
#closing database connection.
if(connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed")

You should get the following output after connecting to PostgreSQL from Python

{'user': 'postgres', 'dbname': 'pynative_DB', 'host': '127.0.0.1', 'port': '5432', 'tty': '', 'options': '', 'sslmode': 'prefe
You are connected to - ('PostgreSQL 10.3')
PostgreSQL connection is closed

https://pynative.com/python-postgresql-tutorial/ 3/9
5/12/2020 Python PostgreSQL Tutorial Using Psycopg2 [Complete Guide]

Understand the PostgreSQL Database connection Code in detail

import psycopg2

This line imports the psycopg2 module in our program. Using the classes and method de ned psycopg2 module we can communicate with
PostgreSQL.

from psycopg2 import Error

Using the Error class of psycopg2, we can handle any database error and exception that may occur while working with PostgreSQL from
Python. Using this approach we can make our application robust.

This module helps us to understand the error in detail. It returns an error message and error code.

psycopg2.connect()

Using the connect() method we can create a connection to a PostgreSQL database instance. This returns a PostgreSQL Connection Object.
this connection is thread-safe and can be shared among many threads.

The connect() method accepts various arguments that we discussed above.

In our example, we passed the following connection arguments to connect PostgreSQL.


user = "postgres", password = "pass@#29", host = "127.0.0.1", port = "5432",database = "postgres_db"

cursor = connection.cursor()

Using connection.cursor() we can create a cursor object which allows us to execute PostgreSQL command through Python source code.

We can create as many cursors as we want from a single connection object. Cursors created from the same connection are not isolated, i.e.,
any changes done to the database by a cursor are immediately visible by the other cursors.

Cursors are not thread-safe.

After this, we printed PostgreSQL Connection properties using a connection.get_dsn_parameters().

cursor.execute()

Using the cursor’s execute method we can execute a database operation or query.

Execute method takes a SQL query as a parameter.

We can retrieve query result using cursor methods such as fetchone(), fetchmany(), fetcthall().

In our example, we are executing a SELECT version(); query to fetch the PostgreSQL version.

try-except- nally block

We placed all our code in the try-except block to catch the database exceptions and errors that may occur during this process.

cursor.close()  and connection.close()

It is always good practice to close the cursor and connection object once your work gets completed to avoid database issues.

Python PostgreSQL Create Table


In this section, we will learn how to create a database table in PostgreSQL from Python code using Psycopg2. I have created an example in
python to create a Mobile table in PostgreSQL.

Steps for creating a table in PostgreSQL in Python

https://pynative.com/python-postgresql-tutorial/ 4/9
5/12/2020 Python PostgreSQL Tutorial Using Psycopg2 [Complete Guide]

Prepare a create table query.

Next, connect to PostgreSQL using a psycopg2.connect(). I have explained the PostgreSQL connection code at the start of this article.

Execute the query using a cursor.execute()

In the end, Close the database connection and cursor object.

Let see the example now.

import psycopg2
from psycopg2 import Error

try:
connection = psycopg2.connect(user = "postgres",
password = "pass@#29",
host = "127.0.0.1",
port = "5432",
database = "postgres_db")

cursor = connection.cursor()

create_table_query = '''CREATE TABLE mobile


(ID INT PRIMARY KEY NOT NULL,
MODEL TEXT NOT NULL,
PRICE REAL); '''

cursor.execute(create_table_query)
connection.commit()
print("Table created successfully in PostgreSQL ")

except (Exception, psycopg2.DatabaseError) as error :


print ("Error while creating PostgreSQL table", error)
finally:
#closing database connection.
if(connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed")

You Should get the following output

Table created successfully in PostgreSQL


PostgreSQL connection is closed

Note: – As you can see we de ne a create table query and passed it to the cursor.execute() function. In the end, we are committing our
changes to the database using the commit() method.

Python PostgreSQL CRUD Operations


We know the table, and it’s column details now let’s move to the crud operations.

In this section, We will learn how to perform PostgreSQL CRUD operations from Python (https://pynative.com/python-postgresql-insert-
update-delete-table-data-to-perform-crud-operations/). Insert, Update, and Delete query from python using Psycopg2 to manipulate the

https://pynative.com/python-postgresql-tutorial/ 5/9
5/12/2020 Python PostgreSQL Tutorial Using Psycopg2 [Complete Guide]

PostgreSQL database.

Let see each section now.

Select data from PostgreSQL Table from Python (https://pynative.com/python-postgresql-select-data-from-table/) – In this article, we
will learn how to execute a PostgreSQL SELECT query from a Python application to fetch data from the database table. We will also learn
how to use fetchall() (https://pynative.com/python-cursor-fetchall-fetchmany-fetchone-to-read-rows-from-table/), fetchmany() and
fetchone() method to fetch limited rows from the table.

Insert data into the PostgreSQL Table from Python (https://pynative.com/python-postgresql-insert-update-delete-table-data-to-


perform-crud-operations/#Python_PostgreSQL_INSERT_into_database_Table) – In this section, We will learn how to execute Insert Query
from python application to insert row or record to the PostgreSQL table.

Update data of PostgreSQL table from Python (https://pynative.com/python-postgresql-insert-update-delete-table-data-to-perform-


crud-operations/#Python_PostgreSQL_UPDATE_Table_Data) – In this section, We will learn how to execute Update Query from python
application to update row or record of a PostgreSQL table.

Delete data from PostgreSQL table from Python (https://pynative.com/python-postgresql-insert-update-delete-table-data-to-perform-


crud-operations/#Python_PostgreSQL_Delete_Table_row_and_columns) – In this section, We will learn how to execute Delete Query from
python application to Delete row or record from a PostgreSQL table.

Call PostgreSQL Function and Stored Procedure in Python

PostgreSQL function and the Stored procedure can perform di erent operations it can be data manipulation or data retrieval. We can execute
such functions from Python.

In this section, we will learn how to execute the PostgreSQL function and Stored procedure in Python. Refer How to execute PostgreSQL
function and stored procedure from Python. (https://pynative.com/python-execute-postgresql-stored-procedure-and-function/)

Python PostgreSQL Transaction management

In this article, we will see how to manage PostgreSQL transactions from Python using psycopg2.

We will see how to use commit and the rollback method of connection class to manage transactions from Python.

We will also see how to change the PostgreSQL transaction isolation level from Python.

Refer our complete guide on How to manage PostgreSQL transaction from Python (https://pynative.com/python-postgresql-transaction-
management-using-commit-and-rollback/).

Python PostgreSQL Connection Pooling

In this section, I will let you know what connection pool is and how to implement a PostgreSQL database connection pool using Psycopg2 in
Python.

Using Psycopg2, we can implement a connection pool for a simple application and also for multithreaded applications.

Use the Connection pool to increase the speed and performance of database-centric applications.

Using Psycopg2, we can implement a simple connection pool for single-threaded applications and a threaded connection pool for a
multithreaded environment.

Refer our complete guide on Python PostgreSQL Connection Pooling Using Psycopg2. (https://pynative.com/psycopg2-python-postgresql-
connection-pooling/)

Python Database Programming Exercise Project

https://pynative.com/python-postgresql-tutorial/ 6/9
5/12/2020 Python PostgreSQL Tutorial Using Psycopg2 [Complete Guide]

Python Database Programming Exercise Project

Solve our Free Python Database Exercise project (https://pynative.com/python-database-programming-exercise-with-solution/) to Practice


and master the Python Database operations using PostgreSQL

In this exercise project, We will implement the Hospital Information System. Which covers above all topics.

In this Python database exercise, we will do database CRUD operations From Python. Exercise also covers transaction
management and error-handling techniques.

Reference: Psycopg – PostgreSQL database adapter for Python (http://initd.org/psycopg/docs/index.html)

That’s it. Folks Let me know your comments in the section below.

Did you nd this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

Tweet (https://twitter.com/intent/tweet/?text=Python+PostgreSQL+Tutorial+Using+Psycopg2&url=https%3A%2F%2F2.gy-118.workers.dev/%3A443%2Fhttps%2Fpynative.com%2Fpython-postgresql-
tutorial%2F&via=PyNative)

F  share (https://facebook.com/sharer/sharer.php?u=https%3A%2F%2F2.gy-118.workers.dev/%3A443%2Fhttps%2Fpynative.com%2Fpython-postgresql-tutorial%2F)

in  share (https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2F2.gy-118.workers.dev/%3A443%2Fhttps%2Fpynative.com%2Fpython-postgresql-
tutorial%2F&title=Python+PostgreSQL+Tutorial+Using+Psycopg2)

P  Pin (https://pinterest.com/pin/create/button/?url=https%3A%2F%2F2.gy-118.workers.dev/%3A443%2Fhttps%2Fpynative.com%2Fpython-postgresql-tutorial%2F&media=https://pynative.com/wp-
content/uploads/2018/08/python_postgresql-1.png&description=Python+PostgreSQL+Tutorial+Using+Psycopg2)

About Vishal
Founder of PYnative.com (https://pynative.com) I am a Python developer and I love to write articles to help developers.
Follow me on Twitter (https://twitter.com/PyNative). All the best for your future Python endeavors!

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

15+ Topic-speci c Exercises and Quizzes  Show All Exercises


Each Exercise contains 10 questions (https://pynative.com/python-exercises-with-solutions/)

Each Quiz contains 12-15 MCQ


   Show All Quizzes  
(https://pynative.com/python-quizzes/)

Is this article/website helpful?

Keep Reading Python


Python Input & Output (https://pynative.com/python-input-function-get-user-input/) Python MySQL (https://pynative.com/python-mysql-tutorial/)
Python PostgreSQL (https://pynative.com/python-postgresql-tutorial/) Python SQLite (https://pynative.com/python-sqlite/)

Python JSON (https://pynative.com/python-json/) Python Quizzes (https://pynative.com/python-quizzes/)


Python Exercises (https://pynative.com/python-exercises-with-solutions/)
Python Generate random data (https://pynative.com/python-random-module/)

https://pynative.com/python-postgresql-tutorial/ 7/9
5/12/2020 Python PostgreSQL Tutorial Using Psycopg2 [Complete Guide]

 Comments
Thank you for reading. Leave a comment below and let us know what do you think of this article.

Follow PYnative
Home (https://pynative.com)

NewsLetter (https://pynative.com/newsletter/)

About Us (https://pynative.com/about-us/)

RSS (https://pynative.com/feed/) | Sitemap (https://pynative.com/sitemap.xml)

Twitter Facebook

(https://twitter.com/PyNative)   (https://www.facebook.com/PyNative-209205313026120/)

Python
Python Tutorials (https://pynative.com/python-tutorials/)

Python Exercises (https://pynative.com/python-exercises-with-solutions/)

Python Quizzes (https://pynative.com/python-quizzes/)

Online Python Code Editor (https://pynative.com/online-python-code-editor-to-execute-python-code/)

Python Tricks (https://pynative.com/useful-python-tips-and-tricks-every-programmer-should-know/)

Join Our Newsletter


Subscribe and Get New Python Tutorials, Exercises, Tips and Tricks into your Inbox Every alternate Week.

Legal Stuff
Privacy Policy (https://pynative.com/privacy-policy/)

Cookie Policy (https://pynative.com/cookie-policy/)

Terms Of Use (https://pynative.com/terms-of-use/)

Contact Us (https://pynative.com/contact-us/)

https://pynative.com/python-postgresql-tutorial/ 8/9
5/12/2020 Python PostgreSQL Tutorial Using Psycopg2 [Complete Guide]

(https://www.dmca.com/Protection/Status.aspx?ID=432bf555-858e-4ab8-bbe9-
165ce5351163&refurl=https://pynative.com/python-postgresql-tutorial/)

Copyright © 2018-2020 · [pynative.com]

AN ELITE CAFEMEDIA TECH PUBLISHER

https://pynative.com/python-postgresql-tutorial/ 9/9

You might also like