[Feature Request] Add support for a pgpass connect parameter
Reported by fpoirotte | May 11th, 2011 @ 03:57 PM
To the best of my knowledge, there is currently no way in psycopg2 to make it use a specific pgpass file instead of the default one, when using keywords with the connect method.
What I'd like to be able to do is:
conn = psycopg2.connect(database="test", user="postgres", pgpass="/etc/testapp/.pgpass")
PostgreSQL accepts that kind of usage (see src/interfaces/libpq/fe-connect.c) since a very long time it seems and so I think it's only natural that psycopg2 accepts it too.
Also, I haven't tried it yet, but I think passing the path to the pgpass file in a conninfo string currently works (ie. psycopg2.connect("dbname=test user=postgres pgpass=/etc/testapp/.pgpass")). However, in my case this is not enough because I use some ORM wrapper which passes those information to psycopg2 as separate keyword arguments (and not as a single conninfo string).
Comments and changes to this ticket
- State changed from new to invalid
I actually don't see the pgpass parameter listed here: http://www.postgresql.org/docs/9.1/static/libpq-connect.html If it is not documented anywhere I don't think we should add it: it may be an implementation detail. Do you have further info?
I see at least two different way to work around the issue:
- write your own wrapper around
psycopg2.connect()taking an arbitrary list of keywords and converting them into a connection string, and tell your ORM to use it (or monkeypatch the
psycopg2module if it doesn't)
- use the
PGPASSFILEenv var that I see used in
src/interfaces/libpq/fe-connect.c: it seems doing what you need
I'm not eager to add all the dozens keywords the libpq supports: passing a connection string seems a good enough solution. You may open a discussion to the mailing list if you think the feature is really useful and check what the other think.
This is in no way an invalid ticket. The use of PGPASS is quite documented:
And it is the standard way to use stored password and connection string options. This is something that should definitely be added. In fact I thought it already worked (because all my other programs can use it) until I started updating PITRTools which is written in Python.
- State changed from invalid to resolved
By the way sometimes after this ticket was closed we have added libpq arguments pass-through (see http://initd.org/psycopg/docs/module.html#psycopg2.connect) so, even if not specifically dealt with, psycopg now does what requested in this ticket (provided that the libpq can handle the 'pgpass' argument in the conninfo string, as it seems the case).
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Psycopg is the most used PostgreSQL adapter for the Python programming language. At the core it fully implements the Python DB API 2.0 specifications. Several extensions allow access to many of the features offered by PostgreSQL.