如何在远程机器上连接postgres和ruby

时间:2012-12-07 04:51:37

标签: ruby-on-rails postgresql centos remote-desktop remote-access

我在带有IP的CentOS的远程机器上,我的postgres在不同的IP上。 我想用我的rails服务器访问postgres。我编辑了database.yml文件并安装了gem pg并在database.yml文件中包含了主机IP ..但我无法运行rake db:migrate和rails服务器..它显示PG错误如下。帮我解决这个问题。我试图从过去4天连接到服务器..但我不能... 当我运行db:migrate时会显示以下错误。

[root@vdimc04 My_Postgres]# rake db:migrate --trace
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
rake aborted!
fe_sendauth: no password supplied
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `new'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `connect'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:326:in `initialize'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:247:in `block (2 levels) in checkout'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `block in checkout'
/usr/local/rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `block in connection'
/usr/local/rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:101:in `connection'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:662:in `initialize'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:570:in `new'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:570:in `up'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:551:in `migrate'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/railties/databases.rake:179:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/task.rb:227:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/task.rb:227:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/task.rb:222:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/task.rb:222:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/task.rb:166:in `block in invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/task.rb:159:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/task.rb:152:in `invoke'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/application.rb:141:in `invoke_task'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/application.rb:99:in `block (2 levels) in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/application.rb:99:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/application.rb:99:in `block in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/application.rb:108:in `run_with_threads'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/application.rb:93:in `top_level'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/application.rb:71:in `block in run'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/application.rb:158:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/lib/rake/application.rb:68:in `run'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/rake-10.0.2/bin/rake:37:in `<top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/bin/rake:19:in `load'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/bin/rake:19:in `<main>'
/usr/local/rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `eval'
/usr/local/rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:migrate

   **my database.yml file**
development:
  adapter: postgresql
  encoding: unicode
  database: db_pchamara
  pool: 5
  username: db_test_user
  password: password
  host: 192.168.1.101
test:
  adapter: postgresql
  encoding: unicode
  database: db_pchamara_test
  pool: 5
  username: db_test_user
password: password


production:
  adapter: postgresql
  encoding: unicode
  database: db_pchamara_production
  pool: 5
  username: db_test_user
password: password

2 个答案:

答案 0 :(得分:2)

它清楚地在本地机器上查找PostgreSQL:

  

服务器是否在本地运行并且正在接受       Unix域套接字“/tmp/.s.PGSQL.5432”上的连接?

你的database.yml应该是这样的:

development:
  adapter: postgresql
  encoding: unicode
  database: blog_development
  pool: 5
  username: blog
  password: very_Secure12/
  host: 224.13.156.78
  port: 5432

您可以在dbserver.myvompany.com行中使用IP或host等主机名。并且它不应包含以socket开头的行。最后,PostgreSQL必须接受来自此服务器的连接。

答案 1 :(得分:1)

根据这个错误:

fe_sendauth: no password supplied

您需要检查远程服务器上pg_hba.conf文件中设置的权限。可能,您不允许远程连接连接到服务器。它看起来应该是这样的,例如:

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD

# "local" is for Unix domain socket connections only
local   all         all                               trust
# IPv4 local connections:
host    all         all         127.0.0.1/32          trust
# IPv6 local connections:
host    all         all         ::1/128               trust
# Remote connections
host    your_db     your_user   (Client IP ADDR)      md5

您可以使用0.0.0.0/0,但这样可以允许来自任何主机的连接。