Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a postgresql server inside a virtualbox created by vagrant.

I have also set up a portforwarding from 5432 inside the box to 15432 on the host system via the Vagrant file.

When connecting via psql

$ psql dbname username -h -p 15432

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

Both, server and client are running Ubuntu 12.04 (postgresql-9.1, Version: 9.1+129ubuntu1)

Connecting inside the VM it self to port 5432 works fine.

The port forwarding it self does not seem to go completely wrong, because when I try to another port, I get "connection refused")

share|improve this question
what does 'ifconfig -a' on the host return? you should have a second (virtual) network adapter for the VBox NAT like '192.168.56.x'. if you replace in your command with the VBox NAT IP address, does it work correctly? when connecting to a vbox nat, its just like connecting to a publicly forwarded port on a gateway router. hosts outside the NAT network use the public IP to connect to it. – Frank Thomas Dec 6 '12 at 15:37
@FrankThomas I have only the normal eth0, lo, wlan0 interfaces. Connecting to SSH and HTTP via forwarded ports and works fine. – Alex Dec 6 '12 at 16:46

Did you configure Postgres to listen on the public interface? By default, it listens only on the loopback adapter

If you're using the Postgres cookbook, you need to set the attribute:

set['postgresql']['config']['listen_addresses'] = '*'

This corresponds to the listen_addresses parameter in postgresql.conf

And likely open up pg_hba to the network that Postgres thinks your connection is coming in on:

Again, the Chef attribute:

set['postgresql']['pg_hba'] = [
    {:type => 'local', :db => 'all', :user => 'all', :addr => nil, :method => 'trust'},
    {:type => 'host', :db => 'all', :user => 'all', :addr => '', :method => 'trust'},
    {:type => 'host', :db => 'all', :user => 'all', :addr => '', :method => 'trust'}
share|improve this answer
great help, especialy the chef bits. – JL Peyret Apr 5 at 5:27
You can tighten this up a bit by being more specific, on the basis of your vm's host ip (in vagrant terms) which you get by node[:network][:default_gateway] in my case, it's, which I also see in ifconfig. then postgres.conf gets listen_addresses = 'localhost,'. – JL Peyret Apr 5 at 5:36
and pg_hba.conf gets host all myuser trust – JL Peyret Apr 5 at 5:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.