10

I am trying to connect to a Postgres Database with variables like this:

cs = "dbname=%s user=%s password=%s host=%s port=%s",(dn,du,dp,dh,dbp)
con = None
con = psycopg2.connect(cs)

However I get the error message:

TypeError: argument 1 must be string, not tuple

I need to be able to use variables in the connection string. Anyone know how to accomplish this?

2 Answers 2

17

Your code currently creates a tuple with your string and the tuple you are trying to sub. You need:

cs = "dbname=%s user=%s password=%s host=%s port=%s" % (dn,du,dp,dh,dbp)
Sign up to request clarification or add additional context in comments.

Comments

3

You could pass params directly without building connection string:

con = psycopg2.connect(
    dbname=dn,
    user=du,
    password=dp,
    host=dh,
    port=dbp,
)

Docs for more details on psycopg2.connect

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.