在 Rails 中通过 database_url 和 Unix Socket 连接到 Postgres [英] Connecting to Postgres Via database_url and Unix Socket in Rails

查看:39
本文介绍了在 Rails 中通过 database_url 和 Unix Socket 连接到 Postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为此四处寻找,但找不到解决方案.

I've hunted around for this and just can't find a solution.

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

Currently I have a database.yml connected to a local pgbouncer server on a Unix socket successfully. However, I'm transitioning to setting this to a database_url environment variable and cannot work out at all how to connect to a local Postgres server via a Unix socket. localhost obviously works OK.

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

I was looking at a URL that looks like this as apparently you can use this with 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 警察:

However, this will not get past the URI police:

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

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

Does anyone have any idea about the secret sauce needed for this? I've Googles endlessly and haven't found anything. It must be possible since the application currently connects over a socket now.

提前致谢,

推荐答案

31.1.1.2.连接 URI

连接 URI 的一般形式是:

The general form for a connection URI is:

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

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

The URI scheme designator can be either postgresql:// or postgres://. Each of the URI parts is optional. The following examples illustrate valid URI syntax uses:

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 分层部分的组件也可以表示为参数.例如:

Components of the hierarchical part of the URI can also be given as parameters. For example:

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

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

Percent-encoding may be used to include symbols with special meaning in any of the URI parts.

任何不符合中列出的关键字的连接参数第 31.1.2 节被忽略,有关它们的警告消息被发送到标准错误.

Any connection parameters not corresponding to key words listed in Section 31.1.2 are ignored and a warning message about them is sent to stderr.

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

For improved compatibility with JDBC connection URIs, instances of parameter ssl=true are translated into sslmode=require.

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

The host part may be either host name or an IP address. To specify an IPv6 host address, enclose it in square brackets:

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

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

The host component is interpreted as described for the parameter host. In particular, a Unix-domain socket connection is chosen if the host part is either empty or starts with a slash, otherwise a TCP/IP connection is initiated. Note, however, that the slash is a reserved character in the hierarchical part of the URI. So, to specify a non-standard Unix-domain socket directory, either omit the host specification in the URI and specify the host as a parameter, or percent-encode the path in the host component of the URI:

postgresql:///dbname?host=/var/lib/postgresql

postgresql://%2Fvar%2Flib%2Fpostgresql/dbname

这篇关于在 Rails 中通过 database_url 和 Unix Socket 连接到 Postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆