Matt
Matt

Reputation: 789

Install psycopg2 on Ubuntu

I'm trying to get the python postgres client module installed on Ubuntu 12.04. The guidance is to do the following:

apt-get install python-psycopg2

However, apt says that the package can't be located.
I'm keen to install this through apt. Is this part of another package that I can install?

Upvotes: 72

Views: 192180

Answers (10)

Oleg Kupavtsev
Oleg Kupavtsev

Reputation: 59

I'm using this in venv, should be the same without venv:

  • Windows:
pip install psycopg2
  • Ubuntu:
pip install psycopg2-binary

E.g.:

pip install psycopg2-binary==2.9.9

If you don't have enough rights:

sudo pip install psycopg2-binary==2.9.9

Upvotes: 2

Snehasish Char
Snehasish Char

Reputation: 23

sudo apt-get install libpq-dev python-dev followed by- pip install psycopg2

Upvotes: 1

Zags
Zags

Reputation: 41328

You should install the library using pip, but this requires multiple system dependencies:

sudo apt-get install libpq-dev python3-dev

Then you can run

pip install psycopg2

Debugging

If you are missing libpq-dev, you will get the error:

Error: pg_config executable not found.

If you are missing python3-dev, you will get the error:

fatal error: Python.h: No such file or directory

Upvotes: 6

akpp
akpp

Reputation: 798

According to the official documentation https://www.psycopg.org/install/

sudo apt install python3-dev libpq-dev
pip install psycopg2

Upvotes: 8

Martin Alexandersson
Martin Alexandersson

Reputation: 1437

This worked for me:

pip install psycopg2-binary

Upvotes: 12

Ilya Sheroukhov
Ilya Sheroukhov

Reputation: 611

Use

sudo apt-get install python3-psycopg2

for Python3 )

Upvotes: 49

azhar22k
azhar22k

Reputation: 5104

I updated my requirements.txt to have psycopg2==2.7.4 --no-binary=psycopg2 So that it build binaries on source

Upvotes: 4

noomz
noomz

Reputation: 2065

I prefer using pip in case you are using virtualenv:

  1. apt install libpython2.7 libpython2.7-dev
  2. pip install psycopg2

Upvotes: 2

Renato Prado
Renato Prado

Reputation: 4150

This works for me in Ubuntu 12.04 and 15.10

if pip not installed:

sudo apt-get install python-pip

and then:

sudo apt-get update
sudo apt-get install libpq-dev python-dev
sudo pip install psycopg2

Upvotes: 142

Jon Clements
Jon Clements

Reputation: 142216

Using Ubuntu 12.04 it appears to work fine for me:

jon@minerva:~$ sudo apt-get install python-psycopg2
[sudo] password for jon: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  python-psycopg2-doc
The following NEW packages will be installed
  python-psycopg2
0 upgraded, 1 newly installed, 0 to remove and 334 not upgraded.
Need to get 153 kB of archives.

What error are you getting exactly? - double check you've spelt psycopg right - that's quite often a gotcha... and it never hurts to run an apt-get update to make sure your repo. is up to date.

Upvotes: 50

Related Questions