2

到目前为止,在我的 rails 应用程序(v 3.2.8)中,我使用迁移没有问题。

我使用 PostgreSQL 9.2 作为数据库。我调整了我application.rb的使用 sql 而不是模式转储程序(未注释config.active_record.schema_format = :sql)。

之后,我开始在迁移时收到此错误:

    $ rake db:migrate

    [ALL MIGRATION STUFF IS PRINTED HERE]

    pg_dump: [archiver (db)] connection to database "my_dev_db" failed: could not connect to server: Connection refused
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?
    could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?
    could not connect to server: Connection refused
        Is the server running on host "localhost" (fe80::1) and accepting
        TCP/IP connections on port 5432?
    rake aborted!
    Error dumping database

I tried manually on the command line(logged in as the same user on my Mac)

<!-- language: lang-sh -->

    $ pg_dump my_dev_db > /tmp/db.sql

No problems with that...happily dumps into `/tmp/db.sql`

Why is rails having trouble with `pg_dump`? (I am on Mac OSX Lion)

===========

Adding more diagnosis information

===========

    $tail -10 /usr/local/var/postgres9.2/pg_hba.conf 

    local   all             all                                     md5
    # IPv4 local connections:
    host    all             all             127.0.0.1/32            md5
    # IPv6 local connections:
    host    all             all             ::1/128                 md5
    # Allow replication connections from localhost, by a user with the
    # replication privilege.
    #local   replication     rogert                                trust
    #host    replication     rogert        127.0.0.1/32            trust
    #host    replication     rogert        ::1/128                 trust


    $ sudo lsof -p 62444 | awk '$5 == "unix" && $NF ~ /\// { print $NF }'
    /tmp/.s.PGSQL.5432

    $ ps auxw | grep post
    postgres        1403   0.0  0.0  2435492    640 s007  S+   21Oct12   0:00.05 bash
    root            1401   0.0  0.0  2498096    128 s007  S    21Oct12   0:00.02 su postgres
    rogert  62517   0.0  0.0  2426700    388 s001  R+    9:21PM   0:00.00 grep post
    rogert  62448   0.0  0.0  2481656    500   ??  Ss    8:46PM   0:00.03 postgres: wal writer process     
    rogert  62447   0.0  0.0  2481656    752   ??  Ss    8:46PM   0:00.07 postgres: writer process     
    rogert  62446   0.0  0.0  2481656   1040   ??  Ss    8:46PM   0:00.00 postgres: checkpointer process     
    rogert  62444   0.0  0.1  2481656   5368 s001  S     8:46PM   0:00.02 /usr/local/Cellar/postgresql/9.2.1/bin/postgres -D /usr/local/var/postgres9.2


    $ rake db:migrate --trace

    [ALL MIGRATION STUFF IS PRINTED HERE]

    /Users/rogert/.rvm/gems/ruby-1.9.3-p194@rails_3.2.8/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:393:in `block (3 levels) in <top (required)>'
    /Users/rogert/.rvm/gems/ruby-1.9.3-p194@rails_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
    /Users/rogert/.rvm/gems/ruby-1.9.3-p194@rails_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
    /Users/rogert/.rvm/gems/ruby-1.9.3-p194@rails_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
    /Users/rogert/.rvm/gems/ruby-1.9.3-p194@rails_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
    /Users/rogert/.rvm/gems/ruby-1.9.3-p194@rails_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
    /Users/rogert/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
    /Users/rogert/.rvm/gems/ruby-1.9.3-p194@rails_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
    /Users/rogert/.rvm/gems/ruby-1.9.3-p194@rails_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
    /Users/rogert/.rvm/gems/ruby-1.9.3-p194@rails_3.2.8/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:162:in `block (2 levels) in <top (required)>'
    /Users/rogert/.rvm/gems/ruby-1.9.3-p194@rails_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
    /Users/rogert/.rvm/gems/ruby-1.9.3-p194@rails_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
    /Users/rogert/.rvm/gems/ruby-1.9.3-p194@rails_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
    /Users/rogert/.rvm/gems/ruby-1.9.3-p194@rails_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
    /Users/rogert/.rvm/gems/ruby-1.9.3-p194@rails_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
    /Users/rogert/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'

$sudo vi /usr/local/var/postgres9.2/postgresql.conf

[search for listen address]

# - Connection Settings -

listen_addresses = 'localhost'          # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
port = 5432                             # (change requires restart)
max_connections = 100                   # (change requires restart)

奇怪的是,如果我从我的 rails 应用程序中完全删除这两行,迁移工作。因此,如果它是 TCP 连接和侦听的问题,迁移本身是如何工作的(但一旦我再次打开这两个就不会)

  1. application.rb - config.active_record.schema_format = :sql
  2. 在我的一个迁移文件中 - t.hstore :attributes
4

3 回答 3

5

很可能您的 PostgreSQL 实例配置为不侦听 TCP/IP,至少在 localhost 上。

postgresql.conf检查listen_addresses中。请参阅文档。很可能它设置为''(空字符串),因此服务器只侦听 UNIX 域套接字。

psqlpg_dump如果 Pg 没有在 TCP/IP 上侦听,它们仍然可以工作,因为它们默认连接到本地 unix 域套接字。Ruby pggem 是一个包装器libpq,与 etc 使用的客户端库相同psql,并且它还默认使用 unix 域套接字,除非显式指定连接参数。

然而,Rails 似乎将一个明确的 IP 地址传递给pg_dump- 导致它尝试通过 TCP/IP 进行连接,而 Pg 似乎没有在监听,从而导致观察到的“连接被拒绝”错误。

或者,您的 PostgreSQL 可能被编译为默认到 5432 以外的端口。相同的设置被编译为 libpq 中的默认值,因此它会自动连接到新端口。但是,如果 Rails 在尝试运行它时指定了一个明确的端口pg_dump,它将优先使用该端口而不是内置默认值。检查port指令postgresql.conf;如果它没有被注释掉或设置为5432这可能是你的问题。该port指令记录在与上面链接的同一页面中。

顺便说一句,在 Pg 运行时定位运行postgresql.confpsql template1 -c "SHOW config_file;"

于 2012-11-01T04:21:39.327 回答
1

根本原因是第 3 方防火墙包认为过滤本地环回接口上的连接是个好主意。

请参阅评论线程

于 2012-11-01T05:37:52.520 回答
0

pg_dump从命令行将使用 UNIX 域套接字访问 PostgreSQL,而 rails 工具正在尝试创建到 localhost 的 TCP 连接。

查看您的 pg_hba.conf(对我来说,这是在 /var/lib/pgsql/data/ 中)并检查是否有这样一行,其中 md5 表示将使用密码身份验证:

host    all         all         127.0.0.1/32          md5

(如果您要在生产环境中运行它,请确保您完全理解这一点!)

-h localhost您可以通过在运行 pg_dump 时在命令行上显式指定来测试通过 TCP 的连接。如果您对 pg_hba.conf 进行任何更改,请记住重新启动 PostgreSQL。

于 2012-11-01T03:21:57.583 回答