
update_sql = "UPDATE employee SET gender = 'male' WHERE id IN (1,3,5)" Similarly, you can also update database record using cursor. Get the cursor object using the obtained connection above: cur = conn.cursor()Įxecute query with cursor, for example, query employees from employee table: query_sql = "SELECT id, name, gender, age, job FROM public.employee"Įvery row in cursor is a list of database record, so you can get column data conveniently: for row in cur: dsn = "host=".format(your_db_host, your_db_name, your_user, your_password)Ĭonnect to database and get a connection object: conn = nnect(dsn) Like all other database drivers, you need a dsn string to connect to database. Then you can install Psycopg2 using pip3: $ sudo pip3 install Psycopg2 This will install the Python3 version of pip command: pip3. 1.2 Install Psycopg2 by Python pipįirst of all you need Python3 version pip: $ sudo apt-get install python3-pip Make sure add 3 suffix to python, otherwise you will install Psycopg2 for Python2 by default. 1.1 Install Psycopg2 by apt $ sudo apt-get install python3-Psycopg2 Then there are two ways to install Psycopg2 for Python3 on Ubuntu: by apt or by pip. To install Psycopg2 on Ubuntu, make sure you have installed libpq-dev package: $ sudo apt-get install libpq-dev
#Pip install psycopg2 how to
This tutorial will show how to play with PostgreSQL using Psycopg2 on Ubuntu. The current release of Psycopg2 support both Python3 and PostgreSQL9.4.

It is widely used and actively developed.


Psycopg2 is an opensource PostgreSQL driver library in Python.
