简体   繁体   English

尝试在 python 中连接时出现 PostgreSQL 错误:dsn 无效:连接选项“用户名”无效

[英]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.

相关问题 无效的 dsn:无效的连接选项“postgis_extension” - invalid dsn: invalid connection option “postgis_extension” 错误:尝试在Python中读取Excel文件时出现无效模式('rb') - Error: invalid mode ('rb') when trying to read excel file in Python 错误:尝试解决 python 中的 ODE 时出现“标识符中的无效字符” - Error: 'Invalid Character in Idnetifier' when trying to solve ODE's in python 尝试使用Python连接到mysql时,2013年丢失了连接错误 - Getting 2013 lost connection error when trying to connect to mysql with Python Errno 61:尝试连接到python服务器时出现连接拒绝错误 - Errno 61: Connection refused error when trying to connect to python server 尝试使用python中的套接字连接到服务器进行下一次连接时发生错误 - error when trying to connect to server for next the connection using sockets in python 在Python中使用cx_Oracle时收到ORA-01017错误(无效的用户名/密码) - Receiving ORA-01017 error (invalid username/password) when using cx_Oracle in Python 通配符错误-“无效选项” - Wildcard error - “invalid option” 尝试使用 mkdir 时 Python 中的语法无效 - Invalid syntax in Python when trying to use mkdir 语法错误:尝试评论时语法无效 - syntax error: invalid syntax when trying to comment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM