pgdb connection string

J

Johannes Bauer

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(connect_string) -> connection
connect_string = 'host:database:user:password:eek: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(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?

I'm puzzled.

Regards,
Johannes
 
M

Mel

Johannes said:
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:eek: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(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,
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':'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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top