当主机不是本地主机时,PostgreSQL连接字符串(URL postgres://...
)的格式是什么?
当主机不是本地主机时,PostgreSQL连接字符串(URL postgres://...
)的格式是什么?
如果您使用各自语言的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
ApplicationName
而不是 application_name
。我尝试了一个 show application_name;
查询,只有当我在连接字符串中提供 ApplicationName=whatever
时,它才使用我提供的应用程序名称。 - Dave Yarwood以下方法对我有用:
const conString = "postgres://YourUserName:YourPassword@YourHostname:5432/YourDatabaseName";
DATABASE_URL=postgres://{user}:{password}@{hostname}:{port}/{database-name}
UseNpgsql()
的内容。我曾经有点困惑,是应该传输它还是postgres:// URL(我也看到过"postgresql://")。 - CrazyPyroPostgreSQL 的连接 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;
DATABASE_URL=postgresql://username:password@host:port/dtabase_name
DATABASE_URL=postgresql+asyncpg://username:password@host:port/dtabase_name
.env
文件中使用DATABASE_URL
。port
是可选项,如果使用默认端口则无需填写。这样一来,您可以连接本地和远程数据库。当您想要检查远程已部署版本中发生的问题时,请考虑这一点。
例如,localhost的DATABASE_URL如下:
DATABASE_URL=postgresql+asyncpg://postgres:dina@localhost/mysens
uri
来连接到您的数据库。DATABASE_URL=postgresql+asyncpg://sqnalxxxxxxxxx:160xxxx2bdd2942b26c93c392xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@ec2-35-173-91-114.compute-1.amazonaws.com:5432/del6o4cjoqfsov
连接字符串还可以从工作的数据库连接器中以编程方式检索。
例如,我有时会从SQLAlchemy的engine
中提取连接字符串,就像这样:
> db_engine.url
postgres://{user}:{password}@{host}:{port}/{db_name}?sslmode=require
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}
有些人似乎将数据库名称误读为服务器名称,将主机误认为是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,而无需像这样编写它。