PostgreSQL连接字符串/URL的格式是什么?

668

当主机不是本地主机时,PostgreSQL连接字符串(URL postgres://...)的格式是什么?


1
此链接提供有关连接字符串、驱动程序类和驱动程序库的信息。https://docs.oracle.com/cd/E19509-01/820-3497/agqka/index.html 此外,要下载最新的jar文件,请使用此链接:https://jdbc.postgresql.org/download.html - Agent 0
这可能会有用:https://www.prisma.io/docs/concepts/database-connectors/postgresql#connection-details - Aniruddha
8
好的,请提供需要翻译的内容。 - Eric
有关pgAdmin的相关问题:https://dev59.com/C7roa4cB1Zd3GeqPoaVI#kfIwoYgBc1ULPQZFz91v - Ryan
9个回答

972

如果您使用各自语言的Libpq绑定,请根据其文档,URI将如下所示组成:

postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]

以下是相同文档的示例

postgresql://
postgresql://localhost
postgresql://localhost:5432
postgresql://localhost/mydb
postgresql://user@localhost
postgresql://user:secret@localhost
postgresql://other@localhost/otherdb?connect_timeout=10&application_name=myapp
postgresql://localhost/mydb?user=other&password=secret

86
这对我有用 postgres://user:secret@localhost:5432/mydatabasename - George
12
"postgresql://localhost/mydb?user=other&password=secret" 这行代码解决了问题。 (翻译说明:尽可能保留原文意思的前提下,将其翻译成通俗易懂的中文,不包含额外的解释或信息。) - Karuhanga
1
如果您仍然遇到问题,请检查密码中的特殊字符,暂时将其更改为仅数字,并测试URL(只是为了验证您的连接是否按预期工作)。 - Edenshaw
11
补充一下@Edenshaw有关特殊字符的注释,密码需要进行URL编码。我刚刚遇到了一个包含“@”字符的密码问题(将其替换为%40解决了这个问题)。 - Tom Hemmes
很奇怪,我认为应该是 ApplicationName 而不是 application_name。我尝试了一个 show application_name; 查询,只有当我在连接字符串中提供 ApplicationName=whatever 时,它才使用我提供的应用程序名称。 - Dave Yarwood
显示剩余3条评论

241

以下方法对我有用:

const conString = "postgres://YourUserName:YourPassword@YourHostname:5432/YourDatabaseName";

如何在 Ruby 中使用此连接字符串? - Amar Prakash Pandey
11
“postgres://”和“postgresql://”是可以互换使用的吗? - Ryu S.
6
URI方案设计符可以是postgresql://或postgres://。 - Grant Oberhauser
1
如果您的主机名是一个URL,您可以使用“ping hostname”来获取IP地址。 - Akansh
谢谢,这是最简单的答案。谢谢 @Hemadri Dasari - undefined

125
DATABASE_URL=postgres://{user}:{password}@{hostname}:{port}/{database-name}

38

这里是JDBC的文档,通用URL是"jdbc:postgresql://host:port/database"

第三章在这里记录了ADO.NET连接字符串,通用连接字符串为Server=host;Port=5432;User Id=username;Password=secret;Database=databasename;

PHP文档在此处,通用连接字符串为host=hostname port=5432 dbname=databasename user=username password=secret

如果您使用其他工具,请告诉我们。


2
感谢您。ADO.NET格式也是您需要传递给Entity Framework Core的UseNpgsql()的内容。我曾经有点困惑,是应该传输它还是postgres:// URL(我也看到过"postgresql://")。 - CrazyPyro
libpq(官方PostgreSQL客户端库)可以理解URL形式和name=value对形式。如果您的连接最终不是通过libpq完成的,则请查阅您的框架文档。 - Jasen
第一个链接已经失效(JDBC文档)。 - tomermes

11

PostgreSQL 的连接 URL 语法:

"Server=host ipaddress;Port=5432;Database=dbname;User Id=userid;Password=password;

例子:

"Server=192.168.1.163;Port=5432;Database=postgres;User Id=postgres;Password=root;

5
  • 数据库 URL 的一般格式
DATABASE_URL=postgresql://username:password@host:port/dtabase_name

如果您正在使用 asyncpg 进行 postgresql sql,则数据库的 URL 为:
DATABASE_URL=postgresql+asyncpg://username:password@host:port/dtabase_name

请记住,永远不要将您的数据库密码推送到代码库中,应该在.env文件中使用DATABASE_URLport是可选项,如果使用默认端口则无需填写。

这样一来,您可以连接本地和远程数据库。当您想要检查远程已部署版本中发生的问题时,请考虑这一点。

例如,localhost的DATABASE_URL如下:

DATABASE_URL=postgresql+asyncpg://postgres:dina@localhost/mysens

如果您在Heroku上部署了数据库,并且想要将其与本地应用程序连接起来,请转到Heroku安装的Postgres附加组件,然后转到设置并点击“Database Credentials”中的“View Credential”,使用uri来连接到您的数据库。
DATABASE_URL=postgresql+asyncpg://sqnalxxxxxxxxx:160xxxx2bdd2942b26c93c392xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@ec2-35-173-91-114.compute-1.amazonaws.com:5432/del6o4cjoqfsov

enter image description here


4

连接字符串还可以从工作的数据库连接器中以编程方式检索。

例如,我有时会从SQLAlchemy的engine中提取连接字符串,就像这样:

> db_engine.url
postgres://{user}:{password}@{host}:{port}/{db_name}?sslmode=require

4
server.address=10.20.20.10
server.port=8080
database.user=username
database.password=password
spring.datasource.url=jdbc:postgresql://${server.address}/${server.port}?user=${database.user}&password=${database.password}

无效的定义连接 URI 方式。 - Ankur Loriya
这不是Postgress的有效方法。 - Golinmarq
数据库名称怎么样?为什么数据库和服务器在同一个端口上? - parsecer

0

有些人似乎将数据库名称误读为服务器名称,将主机误认为是postgresql服务器? 主机托管具有数据库的postgresql服务器。 或者我错过了什么。

postgresql://my_host/&server=my_postgresql_server?user=my_user&port=my_port&password=my_password&database=my_database

例如:

my_host:可以是“localhost”(但这不在问题中),也可以是主机的IP地址。

postgresql://my_host/&server=postgres?user=postgres&port=5432&password=postgres&database=test_db

在我使用Python和运行在本地主机上的postgresql时,它对我起作用。需要sqlalchemy、postgresql和psycopg2才能使其正常工作。

附:该问题涉及到postgres://... URL,但在此处无法使用。相反,您需要使用postgresql,最终在Python中运行的是方言+驱动程序(请参见Database URLs)= postgresql+psycopg2,而无需像这样编写它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接