psycopg2 python 3 tutorial for beginners: The PostgresSQL is the relational database used to create, read, and update a delete the web application data.
The database driver will serve as the client for access to the postgresSQL server.
It is designed for multithreading applications and creates the cursor.
The psycopg 2 is implemented in c as libpq which results in both a secure and efficient way.
Here we can install psycopg by using pip to download from PyPI,
$pip install psycopg2
Or we can use setup.py as a source package,
$python setup.py build
$sudo python setup.py install
We can get the stand-alone package by installing the psycopg2 from PyPI,
$pip install psycog2-binary
The package is used for development and testing also to use the package built from sources.
It can be integrated with Python by using psycopg2 module.
$yum install python-psycope2
Then we use psycopg2 module as we create the connection of the object that represents the database.
S. No. |
API & Description |
2 |
connection.cursor ():-it routine creates a cursor that will be used throughout your database programming using Python. |
3 |
cursor. execute(sql [, optional parameters]) |
4 |
cursor.executemany(sql, seq_of_parameters):- |
5 |
cursor.callproc(procname[, parameters]) |
6 |
cursor.rowcount |
7 |
connection. Commit() |
8 |
connection.rollback() |
9 |
connection.close() |
10 |
cursor.fetchone() |
11 |
cursor.fetchmany([size=cursor.arraysize]) |
12 |
cursor.fetchall() |
The code will show how to connect to an existing database.
If the database does not exist then it will be created and object will be returned.
Import psycopg2
Conn=psyvopg2.connect (database=”tstdb”, user=”pstgr”, password=”pass123”, host=”127.0.0.1”, port=”5432”)
Print “opened database successfully”
Psycopg is released under GNU Lesser General Public License which allows the use of both free and proprietary software.
Psycopg features: -