Python PostgreSQL Tutorial Using Psycopg2 (Complete Guide)
Python PostgreSQL Tutorial Using Psycopg2 (Complete Guide)
Python PostgreSQL Tutorial Using Psycopg2 (Complete Guide)
Tutorials
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
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.
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.
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]
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.
You can also install a speci c version using the following command.
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
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.
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”.
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.
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")
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]
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.
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.
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.
cursor.execute()
Using the cursor’s execute method we can execute a database operation or query.
We can retrieve query result using cursor methods such as fetchone(), fetchmany(), fetcthall().
We placed all our code in the try-except block to catch the database exceptions and errors that may occur during this process.
It is always good practice to close the cursor and connection object once your work gets completed to avoid database issues.
https://pynative.com/python-postgresql-tutorial/ 4/9
5/12/2020 Python PostgreSQL Tutorial Using Psycopg2 [Complete Guide]
Next, connect to PostgreSQL using a psycopg2.connect(). I have explained the PostgreSQL connection code at the start of this article.
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()
cursor.execute(create_table_query)
connection.commit()
print("Table created successfully in PostgreSQL ")
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.
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.
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.
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/)
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/).
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/)
https://pynative.com/python-postgresql-tutorial/ 6/9
5/12/2020 Python PostgreSQL Tutorial Using Psycopg2 [Complete Guide]
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.
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!
Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.
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/)
Twitter Facebook
(https://twitter.com/PyNative) (https://www.facebook.com/PyNative-209205313026120/)
Python
Python Tutorials (https://pynative.com/python-tutorials/)
Legal Stuff
Privacy Policy (https://pynative.com/privacy-policy/)
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/)
https://pynative.com/python-postgresql-tutorial/ 9/9