Ruby on rails:连接到外部Postgresql数据库

时间:2016-01-09 20:35:27

标签: ruby-on-rails postgresql external

我是Ruby on rails的新手。我创建了我的rails项目,我想连接到现有的postgresql数据库(我工作的公司),然后在我的网络应用程序中显示一些数据。 任何人都可以帮忙解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

这些说明假设您使用的是某些版本的Linux。但是,它们在其他操作系统上非常相似。

添加' postgresql'宝石到您的Gemfile:

 gem 'pg'

然后在应用程序的根目录中打开一个终端窗口并运行:

 bundle install

编辑postgresql.conf(位于远程postgresql服务器上)并找到以下行:

 #listen_addresses = 'localhost'

删除评论并将其更改为:

 listen_addresses = '192.168.0.14, localhost'

替换' 192.168.0.14'使用你的Rails应用程序的IP。

现在打开pg_hba.conf(位于远程postgresql服务器上)并向下滚动到:

 # Put your actual configuration here

直接在下面输入你的配置:

 # TYPE   DATABASE      USER        ADDRESS        METHOD
 local    all           all         localhost      md5
 host     all           your_user   192.168.0.14   md5

保存这两个文件后,运行命令:

 sudo service postgresql restart

现在编辑您的Rails应用程序的config / database.yml:

 production:
   adapter: postgresql
   encoding: utf8
   database: the_database_name
   username: your_user
   password: your_database_password
   host: 192.168.0.14
   port: 5432
   pool: 10

 development:
   adapter: postgresql
   encoding: utf8
   database: the_database_name
   username: your_user
   password: your_database_password
   host: 192.168.0.14
   port: 5432
   pool: 10

更改' the_database_name',' your_user'和' your_database_password'到适当的值。

在那之后,你应该做得很好。