无法连接到Vagrant Box上的Postgres – 连接被拒绝?

首先,我是Vagrant和Postgres的新手。

我用http://files.vagrantup.com/lucid32.box创build了我的Vagrant实例,没有任何问题。 我可以运行vagrant upvagrant ssh没有问题。

我按照说明做了一个小改动,我安装了“postgresql-8.4-postgis”包而不是“postgresql postgresql-contrib”。

我开始使用服务器:

 postgres@lucid32:/home/vagrant$ /etc/init.d/postgresql-8.4 start 

虽然连接到stream浪的实例,我可以使用psql连接到实例没有问题。

在我的Vagrantfile中,我已经添加了:

 config.vm.forward_port 5432, 5432 

但是当我尝试从本地主机运行psql时,我得到:

 psql: could not connect to server: Connection refused Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"? 

我确定我缺less一些简单的东西。 有任何想法吗?

更新:

我发现这样的问题的参考和文章build议使用:

 psql -U postgres -h localhost 

与我得到:

 psql: server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. 

你提到的博客文章的说明对于ubuntu来说是不对的:他们使用安装了自编的服务器的部分,这些服务器与打包的版本不能很好地结合。

不应该创build/ usr / local / pgsql / data并运行initdb到该目录,因为ubuntu软件包使用/var/lib/postgresql/<pg-version-number>/<cluster-name>并且代表用户。

提到“/tmp/.s.PGSQL.5432”的错误表明这个文件的预期位置是不正确的(对于ubuntu)。 它应该在/var/run/postgresql 。 这可能是由于使用与ubuntu不兼容的参数手动运行initdb所致。

要编辑以启用非本地连接的postgresql.conf和pg_hba.conf文件应位于/etc/postgresql/8.4/main ,而不是/ usr / local / pgsql / data。

/etc/init.d/postgresql-8.4应该由root(和/etc/init.d中的所有其他)启动,而不是由postgres用户启动。

PGD​​ATA不应该手动设置,因为它再次真正阻碍了ubuntu postgres包的工作方式。

我的答案是清除并重新安装postgresql-8.4包,而不遵循博客文章中的任何指示。 postgresql-8.4-postgis依赖于postgresql-8.4,所以它也会被删除。 还要确保撤消/etc/bash.bashrc中的PGDATA设置。

你可能会发现我的食谱有用。 我刚刚发布在github上。 它使用PostgreSQL 9.1configurationUbuntu 12.04 LTS。