1

我制作了一个简单的编码 ruby​​ 程序,用于从远程服务器获取备份,并在 postgresql 中制作了一个数据库,用于保存信息和安排备份。

与数据库的连接是使用 ActiveRecord 完成的,我配置为访问内部数据库(在其他服务器中),但是当我尝试连接到远程数据库时,我收到以下消息:

/usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:968:in `initialize': FATAL:  no existe el rol <<mi_user_name>> (PGError)
    from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:968:in `connect'
    from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:968:in `connect'
    from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:217:in `initialize'
    from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:37:in `new'
    from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:37:in `postgresql_connection'
    from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:223:in `send'
    from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:223:in `new_connection'
    from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:245:in `checkout_new_connection'
    from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:188:in `checkout'
    from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:184:in `loop'
    from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:184:in `checkout'
    from /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
    from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:183:in `checkout'
    from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:98:in `connection'
    from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:326:in `retrieve_connection'
    from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_specification.rb:123:in `retrieve_connection'
    from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_specification.rb:115:in `connection'
    from /usr/lib/ruby/1.8/active_record/base.rb:3113:in `quoted_table_name'
    from /usr/lib/ruby/1.8/active_record/base.rb:1684:in `construct_finder_sql'
    from /usr/lib/ruby/1.8/active_record/base.rb:1548:in `find_every'
    from /usr/lib/ruby/1.8/active_record/base.rb:1505:in `find_initial'
    from /usr/lib/ruby/1.8/active_record/base.rb:613:in `find'
    from main.rb:84:in `main'
    from main.rb:126

PG 尝试使用我的本地用户连接到远程数据库,在 ActiveRecord 的声明中我设置了参数:

require 'active_record'

    ActiveRecord::Base.establish_connection(
        :adapter  => "postgresql",
        :host     => "xxx.xxx.x.101",
        :port     => 5432,
        :database => "VB_DB",
        :user     => "pg_user",
        :password => "blahblah"
    )

我使用 Ruby 1.8.7。

对此有任何想法吗?

感谢阅读,帮助我。

问候。

编辑

我找到了解决方案:

    ActiveRecord::Base.establish_connection(
        :adapter  => "postgresql",
        :host     => "xxx.xxx.x.101",
        :port     => 5432,
        :database => "VB_DB",
        :username => "pg_user",
        :password => "blahblah"
    )

我使用了:user,并且必须是:username。真丢人。

4

1 回答 1

1

从错误消息来看,Postgres 似乎认为您的用户不存在于数据库中。

解决此问题的一个很好的第一步是尝试在不涉及 Ruby 的情况下进行连接。由于“psql”命令行客户端使用相同的底层库(假设您使用“pg”gem 进行连接),通常如果您可以使用它进行连接,那么您也应该可以从 Ruby 进行连接。

尝试这个:

psql -h xxx.xxx.x.101 -U pg_user -W -l

如果这向您显示相同的错误 ( no existe el rol <<mi_user_name>>),那么您需要确认您的用户存在并创建它。

如果它向您显示数据库列表,我们将不得不尝试其他方法。

于 2011-08-22T22:32:37.033 回答