pgdb connection string

Mel mwilson at
Fri Jun 13 17:50:38 CEST 2008

Johannes Bauer wrote:
> I'm trying to initialize a connection to a PG database. So help(pgdb)
> says:
> pgdb.connect(connect_string) -> connection
>              connect_string = 'host:database:user:password:opt:tty'
>              All parts are optional. You may also pass host through
>              password as keyword arguments. To pass a port, pass it in
>              the host keyword parameter:
>                      pgdb.connect(host='localhost:5432')
> Now from what I understand is that it accepts a string in the form:
> "%s:%s:%s:%s" % (conf["db"]["hostname"],
>                   conf["db"]["database"],
>                   conf["db"]["username"],
>                   conf["db"]["password"])
> Which actually works. But if I want to pass the port, there's one more
> colon and it parses garbage. So what exactly is this host="foobar"
> syntax all about? What exactly is passed to pgdb.connect (because it
> does not seem to be a string) - is it a dictionary or something?

My guess is that pgdb.conf is passed a bunch of things, as though its
signature were

def pgdb (connect_string, host=None, database=None, user=None,

or something similar.  My instinct would be to try

pgdb.connect (':my_database:my_username:my_password', host='localhost:5432')

In my own code, I almost always use a DB-API 2.0 interface and a dict:

db_params = {'database':'my_db', 'host':'dbserver', 'password':"don't tell"}
db = psycopg2.connect (**db_params)

(untested code above)  The dict usually gets patched up from environment
variables and command-line arguments before the connect call.

        Cheers,         Mel.

More information about the Python-list mailing list