pgdb connection string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johannes Bauer

    pgdb connection string

    Hello group,

    I've run into a small problem with pgdb which is actually not PostgreSQL
    specific - I just do not understand the Python syntax at one point.

    I'm trying to initialize a connection to a PG database. So help(pgdb) says:

    pgdb.connect(co nnect_string) -connection
    connect_string = 'host:database: user:password:o pt: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(ho st='localhost:5 432')

    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?

    I'm puzzled.

    Regards,
    Johannes

    --
    "Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
    reicht zu wissen, daß andere es besser können und andere es auch
    besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
    in de.sci.electron ics <47fa8447$0$115 45$9b622d9e@new s.freenet.de>
  • Mel

    #2
    Re: pgdb connection string

    Johannes Bauer wrote:
    I'm trying to initialize a connection to a PG database. So help(pgdb)
    says:
    >
    pgdb.connect(co nnect_string) -connection
    connect_string = 'host:database: user:password:o pt: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(ho st='localhost:5 432')
    >
    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,
    password=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':'dbserve r', 'password':"don 't tell"}
    db = psycopg2.connec t (**db_params)

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

    Cheers, Mel.

    Comment

    Working...