9

So I have got this task: You have two abstract reports made available to you on a PostgreSQL server located at: postgres://candidate.company.org/company

username = candidate password = abc

So I used this code to try connect to the database:

import psycopg2 as db
conn = db.connect(host='postgres://candidate.suade.org/company', database='randomname', user='candidate', password='abc', port='5432')

But then i receiver error message:

ProgrammingError: invalid dsn: invalid connection option "username"

As the username give is correct Can anyone help?

2
  • The connect uses dbname as argument per docs. Commented Dec 21, 2017 at 17:19
  • Please remove invalid tag "mysql". Thanks. Commented Jul 14, 2023 at 11:18

3 Answers 3

6

You are using the wrong parameters. In Postgres you have to use something like this:

conn = pg.DB(host="localhost", user="USERNAME", passwd="PASSWORD", dbname="DBNAME")

That should fix it.

Sign up to request clarification or add additional context in comments.

4 Comments

but dont i have to access postgresql with the psycopg2 on python so with that as i did as i rightly first imported psycopg2 as bd then connected it with conn = db.connect(host="localhost", username="USERNAME", passwd="PASSWORD", dbname="DBNAME")?? or where am i getting it wring here as the error comes ProgrammingError: invalid dsn: invalid connection option "username"
@robertjames it should be user=, not username=.
It should be password and not passwd.
database works for me, unless it is in a string.
3

if you are using psycopg2 library to connect the Postgres DB , then the connection string should be as mentioned below .

conn = psycopg2.connect(host="localhost",**user = "postgres"**,password = "postgres",database = "postgres") ;

It should be user not username .

Comments

2

Please try with below solution

import psycopg2 as db
conn = db.connect(host='localhost', dbname='randomname', user='candidate', password='abc')

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.