2013-05-07 128 views
4

我使用postgresql通过Vagrant虚拟机制作,并希望将其用作我的Rails应用程序数据库。如何在虚拟机上访问postgresql?

我使用类似Vagrantfile:

Vagrant.configure("2") do |config| 
    config.vm.network :private_network, ip: "192.168.33.18" 
    config.vm.provision :chef_solo do |chef| 
    # I take cookbook from https://github.com/opscode-cookbooks/postgresql 
    chef.add_recipe "postgresql::server" 
    chef.json = { 
     postgresql: { 
     config: { 
      ssl: false, 
      listen_addresses: '*' 
     }, 
     password: { 
      postgres: 'postgres' 
     } 
     } 
    } 
    end 
end 

接下来我vagrant up并期待下一次database.yml将工作:

development: 
    database: app_development 
    adapter: postgresql 
    host: '192.168.33.18' 
    username: root 

但这是不会发生.. Rake任务失败,错误:

could not connect to server: Connection refused 
    Is the server running on host "192.168.33.18" and accepting 
    TCP/IP connections on port 5432? 

而且我看到那个端口实际上没有打开

$ nmap -p 5432 192.168.33.18 

Starting Nmap 6.25 (http://nmap.org) at 2013-05-07 22:05 FET 
Nmap scan report for 192.168.33.18 
Host is up (0.00047s latency). 
PORT  STATE SERVICE 
5432/tcp closed postgresql 

我可以在vm上看到一些postgres进程,并且在vagrant输出中没有任何错误,所以我相信postgres正常工作。

932 postgres 20 0 126m 9.9m 8916 S 0 2.7 0:00.03 

postgres                            
    941 postgres 20 0 97460 1348 192 S 0 0.4 0:00.00 postgres                            
    944 postgres 20 0 126m 1728 548 S 0 0.5 0:00.06 postgres                            
    945 postgres 20 0 126m 1488 316 S 0 0.4 0:00.05 postgres                            
    946 postgres 20 0 126m 2768 1000 S 0 0.7 0:00.00 postgres                            
    947 postgres 20 0 97456 1584 352 S 0 0.4 0:00.00 postgres 

如何设置Vagrant以在我的Rails应用程序中的虚拟机上使用db?

UPDATEpg_hba.conf

# TYPE DATABASE  USER   ADDRESS     METHOD 

# "local" is for Unix domain socket connections only 
local all    all          peer 

########### 
# Other authentication configurations taken from chef node defaults: 
########### 

local all    all          trust 

host all    all    0.0.0.0/0    md5 
+1

也许你需要一些postgresql服务器启动设置?你能提供流浪记录吗? – cmur2 2013-05-07 19:27:08

+0

我更新了我的vagrantfile(实际上它更大,因为还有另外一本食谱)。并且还提供一些来自虚拟机的附加数据 – MikDiet 2013-05-07 19:35:46

+0

Postgres使用名为_pg_hba.conf_的配置文件来控制连接访问。我不知道厨师或流浪汉是如何管理这个文件的;在普通的postgres安装中,你只需编辑它。 – 2013-05-07 19:41:57

回答

6

我道歉,向所有的读者,但问题是在我的注意力不集中。虽然我在配置中指向了listen_addresses: '*',但是我无意中重置了此配置,因此在最终配置中采用了默认值listen_addresses: 'localhost'的值。修复此端口后,打开。

+2

很高兴你解决了。一定要给你的答案复选标记。 – 2013-05-08 15:32:43

相关问题