Install psycopg2 ubuntu : As it is an open-source library PostgreSQL driver is in python language and widely used and developed.
The psycopg2 will support python 3 and PostgreSQL 9.4.
Install psycopg2 by apt. $sudo apt-get install python3-psycopg2
Install psycopg2 by python pip
Firstly we need python3 version pip: $sudo apt-get install python3-pip
The first step is to Install Psycopg2
Then to install Psycopg on ubuntu we have to install libpq-dev package:-
$ sudo apt-get install libpq-dev
Installation of Psycopg2 for Python3 has two ways by Ubuntu:
$ sudo apt-get install python3-Psycopg2
Install Psycopg2 for Python2 by default.
Install Psycopg2 by Python pip:-
We need Python3 version pip:-
$ sudo apt-get install python3-pip
Install the Python3 version of pip command: pip3.
Install Psycopg2 using pip3:-
$ sudo pip3 install Psycopg2
Dsn=”host={}dbname={}user={}password={}”.format(ypur_db_host,your_db_name,ypur_user,your_password)
Then we connect to database and get an object:-
conn = psycopg2.connect (dsn)
Fetch Data Record with a Cursor:-
cur = conn.cursor ()
Sudo apt-get install postgresql libpq-dev postgresql-client postgresql-client-common
The apt will finish downloading, installing and processing.
We use the sudo command to switch to the new "postgres" account.
Sudo-i-u postgres
The PostgreSQL will prompt you with several questions. Answer "n" to superuser and "y" to the other questions.tt-p--interactive
We create a new database we can use for testing and name it "testpython" or anything.
created testpython
The command-line client is used for connecting directly to our PostgreSQL server without any Python code.
Psql testpython
Install psycopg2 python 3 windows:-
Here the configure 64 is bit project for the c++ and we don’t use Microsoft visual studio.
Here to get the 64-bit compiler we download windows SDK for windows 7 and .NET framework and then install.
C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd.
I used the following command to get a command line with the right settings-
C:\windows\system32\cmd.exe/E:ON/V:ON/T:0E/K...C:\programfiles\Microsoft SDKs\Windows\v7.1\Bin\SetWnv.cmd”/RELEASE/x64.
We create a new shortcut on the desktop as you do not have to type it when you want to install new python modules.
Set “PATH=%VIRTUAL_ENV%\Scripts; %PATH%”
“C:\programFiles\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd”/RELEASE/x64
: END
Install Psycopg2:-
We open a command prompt using the above command.
Then we will get windows as,
Then we have to set the Path variable so that pip will find pg_config.exe which is part of PostgreSQL,
Set PATH=%PATH%;C:\ProgramFiles\PostgreSQL\9.3\bin
Pip Install psycopg2
Installation on Windows:-