【问题标题】:Connecting to Postgres Via database_url and Unix Socket in Rails在 Rails 中通过 database_url 和 Unix Socket 连接到 Postgres
【发布时间】:2015-01-18 05:15:57
【问题描述】:

我一直在寻找这个,但找不到解决方案。

目前我有一个 database.yml 成功连接到 Unix 套接字上的本地 pgbouncer 服务器。但是,我正在过渡到将其设置为 database_url 环境变量,并且根本无法解决如何通过 Unix 套接字连接到本地 Postgres 服务器。 localhost 显然可以正常工作。

我正在查看一个看起来像这样的 URL,显然您可以将其与 Postgres (http://www.postgresql.org/docs/9.2/interactive/libpq-connect.html) 一起使用:

export DATABASE_URL="postgresql://username@%Fvar%Frun%Fpostgresql%F.s.PGSQL.6432/dbname"

但是,这不会通过 URI 警察:

rubies/ruby-2.1.5/lib/ruby/2.1.0/uri/common.rb:176:in `split': bad URI(is not URI?): postgresql://username@%Fvar%Frun%Fpostgresql%F.s.PGSQL.6432/dbname

有人知道这需要什么秘方吗?我无休止地用谷歌搜索,没有找到任何东西。这一定是可能的,因为应用程序现在通过套接字连接。

提前致谢,

【问题讨论】:

    标签: rails-activerecord


    【解决方案1】:

    31.1.1.2. Connection URIs

    连接 URI 的一般形式是:

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

    URI 方案指示符可以是 postgresql:// 或 postgres://。 每个 URI 部分都是可选的。以下示例说明 有效的 URI 语法使用:

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

    URI 的分层部分的组件也可以表示为 参数。例如:

    postgresql:///mydb?host=localhost&port=5433
    

    百分比编码可用于包含具有特殊含义的符号 在任何 URI 部分中。

    任何与列表中列出的关键字不对应的连接参数 第 31.1.2 节将被忽略,有关它们的警告消息将发送至 标准错误。

    为了提高与 JDBC 连接 URI 的兼容性, 参数ssl=true 被翻译成sslmode=require

    主机部分可以是主机名或 IP 地址。指定一个 IPv6 主机地址,用方括号括起来:

    postgresql://[2001:db8::1234]/database
    

    主机组件被解释为参数主机的描述。 特别是,如果主机选择了 Unix 域套接字连接 部分为空或以斜杠开头,否则为 TCP/IP 连接已启动。但是请注意,斜杠是保留的 URI 的分层部分中的字符。 所以,要指定一个 非标准 Unix 域套接字目录,或者省略主机 在 URI 中指定并指定主机作为参数,或 对 URI 主机组件中的路径进行百分比编码:

    postgresql:///dbname?host=/var/lib/postgresql
    
    postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
    

    【讨论】:

    • localhost 通过 TCP/IP 连接(127.0.0.1、::1、fe80::1%lo0 等)。要使用像 OP 这样的 Unix 套接字,请省略主机。
    • 谎言谎言!我使用postgresql://user:pass/dbname 无济于事。我已经尝试了上述的许多变体。似乎是rails 3.2.21。我从应用程序(openproject)没有收到任何有意义的错误消息,但 tcp 表单至少记录了 pg 的身份验证错误。 @J.Money
    • @Otheus 我的评论是关于在连接字符串中使用 localhost。由于 localhost 不为空且不以斜杠开头,因此它被解释为主机名并通过 TCP/IP 连接到环回连接。 “如果主机部分为空或以斜杠开头,则选择 Unix 域套接字连接”,因此您仍然需要为非标准套接字指定主机。您的 database.yml 文件中是否有以下内容:host: /path/to/socket/file
    • @J.Money 正确,但省略主机不起作用——但这不是因为libpq。显然 activerecord (3.2.21) 在这里做了一些神奇的事情。它尝试通过URI.parse 自行解析字符串,而不是将其传递给PGconn。 :(
    • 最后一个选项,但将 lib 更改为 run 对我有用:postgresql://%2Fvar%2Flib%2Fpostgresql/dbname。文档也有一些关于连接优先级的好信息:edgeguides.rubyonrails.org/…
    【解决方案2】:

    您必须省略主机才能使用 unix 套接字,如下所示:

    postgres://username@/dbname
    

    或者干脆

    postgres:///dbname
    

    这适用于 psql > 9.2。

    我不确定它是否适用于数据库 URL 的 rails 处理。

    【讨论】:

    • 这不适用于 Rails 4.2.5 和 PostgreSQL 9.3
    • 虽然看似无关,但我错过了我要移植到 Rails 5 的应用程序上的第三个斜线......这解决了我遇到的this 问题。
    • 有趣,添加?您可以添加参数,如 sslmode=disable postgres:///dbname?ssmode=disable
    【解决方案3】:

    您可以将套接字作为查询参数传递:

    postgresql://user@host/database?socket=/path/to/socket
    

    【讨论】:

    • socket 不是大多数 PostgreSQL API 的有效参数
    【解决方案4】:

    如果一般使用libpq,@blnc 提供的答案是正确的。但是,如果您使用 Activerecord 或 RubyonRails,至少在版本 3.2.21 中,该模块会将 URI 解析为 Ruby 的 URI.parse,而不是直接将其交给 libpq。 URI.parse 无法在此处处理空主机名。

    irb(main):020:0> URI.parse "postgresql://user:pass@host/dbname"
    => #<URI::Generic:0x7f72b17f04d8 URL:postgresql://user:pass@host/dbname>
    

    但是,没有主机:

    irb(main):021:0> URI.parse "postgresql://user:pass@/dbname"
    URI::InvalidURIError: the scheme postgresql does not accept registry part: user:pass@ (or bad hostname?)
            from /usr/lib/ruby/1.8/uri/generic.rb:195:in `initialize'
            from /usr/lib/ruby/1.8/uri/common.rb:492:in `new'
            from /usr/lib/ruby/1.8/uri/common.rb:492:in `parse'
            from (irb):21
            from /usr/lib/ruby/1.8/uri/generic.rb:556
    

    如果不添加自定义 URI 解析代码(或更改活动记录),似乎无法解决此问题。

    【讨论】:

      【解决方案5】:

      在 Archlinux 上,unix_socket_directories 的默认路径:

      postgres:///<dbname>?host=/run/postgresql/
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-20
        • 1970-01-01
        • 2014-02-15
        • 1970-01-01
        • 2019-07-10
        • 1970-01-01
        • 2019-02-04
        相关资源
        最近更新 更多