askvictor
askvictor

Reputation: 3819

How can I tell/specify which version of the postgres client/libs psycopg2 is using?

I have psycopg2 for a python program, but the computer the program is running on has two version of postgres installed: 9.2 in the 'main' system, and 9.5 in /opt. The postgres DB is hosted elsewhere, and is 9.5. The 9.2 psql client will still happily connect to the 9.5 DB, with a warning. How can I tell which version or postgres the psycopg2 library is using, and how can I force it to use a specific version? Running SELECT version(); seems to return the server version, while I'm only interested in the client version.

Upvotes: 2

Views: 3099

Answers (3)

piro
piro

Reputation: 13931

In psycopg 2.6.x ldd is the way to go. In 2.7 there will be a function to return that information, but it's not been released yet.

Fast forward many years: since 2.7 you can use the libpq_version() function.

Upvotes: 1

Étienne Bersac
Étienne Bersac

Reputation: 649

A portable solution for psycopg2 before 2.7 is to use ctypes:

def libpq_version():
    try:
        return psycopg2.__libpq_version__
    except AttributeError:
        with open('/proc/self/maps') as fo:
            for line in fo:
                values = line.split()
                path = values[-1]
                if 'libpq.so' in path:
                    break
            else:
                raise Exception("libpq.so not loaded")

        libpq = ctypes.cdll.LoadLibrary(path)
        return libpq.PQlibVersion()

This depends on /proc and thus Linux, afaik.

Upvotes: 1

d1ll1nger
d1ll1nger

Reputation: 1701

Should be able to force it by putting the 9.5 client binaries at the start of the path for the user that is running your python program. To test:

export PATH=/opt/<path-to-your-9.5>;$PATH
which psql #ensure the 9.5 one is returned
psql -h <remote-host> -p <remote-port> -U <user> -W -d <dbname> # test even further if you wish

If you wanting know the lib then you need to look at what libpq your python is using:

[root@pgbuilder cgi-bin]# ldd /usr/lib64/python2.7/site-packages/psycopg2/_psycopg.so | grep libpq
libpq.so.5 => /usr/pgsql-9.5/lib/libpq.so.5 (0x00007fd5f40b7000)

Change your path to suit your "_psycopg.so" location.

Upvotes: 1

Related Questions