当主机不是本地主机时,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
以下对我有用
const conString = "postgres://YourUserName:YourPassword@YourHostname:5432/YourDatabaseName";
DATABASE_URL=postgres://{user}:{password}@{hostname}:{port}/{database-name}
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;
DATABASE_URL=postgresql://username:password@host:port/dtabase_name
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
uri
连接到您的数据库DATABASE_URL=postgresql+asyncpg://sqnalxxxxxxxxx:160xxxx2bdd2942b26c93c392xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@ec2-35-173-91-114.compute-1.amazonaws.com:5432/del6o4cjoqfsov
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}
还可以通过编程方式从工作的数据库连接器中检索连接字符串。
例如,我有时会从 SQLAlchemy 的
engine
中提取连接字符串,如下所示:
> db_engine.url
postgres://{user}:{password}@{host}:{port}/{db_name}?sslmode=require
有些人似乎将数据库名称误读为服务器名称,将主机误读为 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,而不需要这样写。