gpt4 book ai didi

ruby - (Ruby)带有 PG 的 ActiveRecord 尝试将本地用户连接到远程服务器

转载 作者:行者123 更新时间:2023-11-29 13:37:10 24 4
gpt4 key购买 nike

我制作了一个 simple-coding-ruby-program 用于从远程服务器获取备份,我在 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。真丢人。

最佳答案

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

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

试试这个:

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

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

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

关于ruby - (Ruby)带有 PG 的 ActiveRecord 尝试将本地用户连接到远程服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7116670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com