[英]PostgreSQL error when trying to connect in python : invalid dsn: invalid connection option "username"
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所以我完成了这个任务:在位于以下位置的 PostgreSQL 服务器上有两个抽象报告可供您使用:postgres://candidate.company.org/company
username = candidate password = abc用户名 = 候选密码 = 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?由于用户名是正确的,有人可以帮忙吗?
You are using the wrong parameters.您正在使用错误的参数。 In Postgres you have to use something like this:
在 Postgres 中,你必须使用这样的东西:
conn = pg.DB(host="localhost", user="USERNAME", passwd="PASSWORD", dbname="DBNAME")
That should fix it.那应该解决它。
if you are using psycopg2
library to connect the Postgres DB , then the connection string should be as mentioned below .如果您使用
psycopg2
库连接 Postgres DB,则连接字符串应如下所述。
conn = psycopg2.connect(host="localhost",**user = "postgres"**,password = "postgres",database = "postgres") ;
It should be user not username .它应该是 user 而不是 username 。
Please try with below solution请尝试以下解决方案
import psycopg2 as db
conn = db.connect(host='localhost', dbname='randomname', user='candidate', password='abc')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.