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

问题描述 投票:0回答:10

当主机不是本地主机时,PostgreSQL 连接字符串(URL

postgres://...
)的格式是什么?

postgresql database-connection
10个回答
977
投票

如果您对相应语言使用 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

242
投票

以下对我有用

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

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

38
投票

这里是JDBC的文档,一般URL是“jdbc:postgresql://host:port/database”

第 3 章此处记录了 ADO.NET 连接字符串, 一般连接字符串是

Server=host;Port=5432;User Id=username;Password=secret;Database=databasename;

PHP文档我们这里,一般的连接字符串是

host=hostname port=5432 dbname=databasename user=username password=secret

如果您使用其他东西,则必须告诉我们。


11
投票

postgres 的连接 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;

6
投票
  • 数据库url的一般格式
DATABASE_URL=postgresql://username:password@host:port/dtabase_name
  • 如果您将 postgresql sql 与 asyncpg 一起使用,则数据库 url 将是
DATABASE_URL=postgresql+asyncpg://username:password@host:port/dtabase_name
  • 记住永远不要推送您的数据库密码,因此您应该在
    DATABASE_URL
    文件中使用您的
    .env

  • 如果您使用默认的,则
    port
    是可选的
  • 这样,您可以连接本地和远程数据库,想想一旦您想检查远程部署版本中发生的问题

  • 本地主机 DATABASE_URL 的 ex 将是

DATABASE_URL=postgresql+asyncpg://postgres:dina@localhost/mysens
  • 如果您在 Heroku 上部署了数据库并且想要将其与本地应用程序连接,请转至 Heroku Postgres 安装的附加组件,转至设置并单击数据库凭据中的查看凭据,然后使用
    uri
    连接到您的数据库
DATABASE_URL=postgresql+asyncpg://sqnalxxxxxxxxx:160xxxx2bdd2942b26c93c392xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@ec2-35-173-91-114.compute-1.amazonaws.com:5432/del6o4cjoqfsov


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}

4
投票

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

例如,我有时会从 SQLAlchemy 的

engine
中提取连接字符串,如下所示:

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

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 中为我工作,运行 sqlalchemy 和 postgresql 本地主机。需要 sqlalchemy、postgresql 和 psycopg2 才能使其工作。

PS:问题是关于 postgres://... URL,但这在这里不起作用。相反,你需要postgresql,而Python中最终运行的是dialect+driver(参见Database URLs)=postgresql+psycopg2,而不需要这样写。


0
投票

添加更多:如果您想查找 url 的值,请在 pgAdmin 面板

© www.soinside.com 2019 - 2024. All rights reserved.