【问题标题】:What is the format for the PostgreSQL connection string / URL?PostgreSQL 连接字符串/URL 的格式是什么?
【发布时间】:2011-04-04 16:43:43
【问题描述】:

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

【问题讨论】:

标签: postgresql database-connection


【解决方案1】:

如果对各自的语言使用 Libpq 绑定,则根据其documentation 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

【讨论】:

  • 这对我有用 postgres://user:secret@localhost:5432/mydatabasename
  • postgresql://localhost/mydb?user=other&password=secret 成功了
  • 如果您仍然有问题,请检查密码中的特殊字符,暂时将其更改为仅数字并测试 URL(只是为了验证您的连接是否按预期工作)
  • 我的问题是简单地从 DataGrip 中复制“jdbc:postgres:// ...”字符串。不幸的是,错误消息没有帮助。谢谢!
  • 要添加到@Edenshaw 关于特殊字符的注释,密码需要进行 url 编码,我只是偶然发现了这个问题,密码包含 '@' 字符(用 %40 替换它解决了它)
【解决方案2】:

以下对我有用

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

【讨论】:

  • 如何在ruby中使用这个连接字符串?
  • 'postgres://' 和 'postgresql://' 可以互换吗?
  • @RyuS。 The URI scheme designator can be either postgresql:// or postgres:// 从这里:postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
  • 如果你的主机名是一个url,你可以使用ping hostname来获取IP地址。
【解决方案3】:
DATABASE_URL=postgres://{user}:{password}@{hostname}:{port}/{database-name}

【讨论】:

    【解决方案4】:

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

    第 3 章 here 记录了 ADO.NET 连接字符串, 一般连接字符串为Server=host;Port=5432;User Id=username;Password=secret;Database=databasename;

    我们的PHP文档here,一般的连接字符串是 host=hostname port=5432 dbname=databasename user=username password=secret

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

    【讨论】:

    • 谢谢。 ADO.NET 格式也是您需要传递给 Entity Framework Core 的 UseNpgsql() 的内容。我有点困惑是应该是那个还是 postgres:// URL(我也将其视为“postgresql://”)
    • libpq(官方 postgresql 客户端库)理解 URL 形式和 name=value 对形式。如果您最终不是通过 libpq 进行连接,请查阅您的框架的文档。
    【解决方案5】:

    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】:
      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}
      

      【讨论】:

        猜你喜欢
        • 2010-10-20
        • 1970-01-01
        • 2010-09-06
        • 2010-09-21
        • 1970-01-01
        • 2013-11-09
        • 1970-01-01
        • 2016-06-18
        相关资源
        最近更新 更多