How to Fix “Connection Attempt Failed” When Connecting to PostgreSQL on VirtualBox Guest from Host System


4 views

When working with PostgreSQL in a VirtualBox environment, connection issues between host and guest systems are common but solvable. The typical symptoms include:

  • "Connection attempt failed" errors
  • Timeout messages
  • Authentication failures despite correct credentials

The VirtualBox port forwarding setup appears correct in your configuration, but let's verify the complete command syntax:

VBoxManage modifyvm "vmname" --natpf1 "pgsql,tcp,,5432,,5432"

Alternatively, you can set it through the VirtualBox GUI:

  1. Right-click your VM → Settings → Network
  2. Expand Advanced → Port Forwarding
  3. Add rule: Name=pgsql, Protocol=TCP, Host IP=blank, Host Port=5432, Guest IP=blank, Guest Port=5432

Your pg_hba.conf needs additional considerations. Let's enhance it:

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
host    all         all         0.0.0.0/0             md5
host    all         all         ::/0                  md5
local   all         all                              peer

Also check postgresql.conf for these critical settings:

listen_addresses = '*'
port = 5432
max_connections = 100

The network adapter type matters in VirtualBox. For PostgreSQL connectivity:

  • Use NAT with port forwarding for simplest setup
  • Bridged adapter for direct network access
  • Host-only for secure internal networks

Run these diagnostic commands in sequence:

# On guest system:
sudo netstat -tulnp | grep 5432
sudo ufw status
ping 10.0.2.2

# On host system:
telnet localhost 5432
ping [guest-ip]

For different connection scenarios:

# Standard connection from host:
psql -h localhost -p 5432 -U username -d dbname

# Using application connection strings:
# JDBC: jdbc:postgresql://localhost:5432/dbname
# Python: psycopg2.connect(host="localhost", port=5432, user="user", password="pwd", database="db")

If problems persist, check these advanced areas:

  1. Verify VirtualBox network adapter driver version
  2. Test with different PostgreSQL authentication methods (trust/md5)
  3. Try alternate ports to rule out conflicts
  4. Examine VirtualBox logs (VBox.log in VM folder)

First, let's ensure your VirtualBox port forwarding is correctly set up. Run this command in your host's command prompt:

vboxmanage showvminfo "Your_VM_Name" | find "NAT"

You should see output similar to:

NIC 1 Rule(0):   name = pgsql, protocol = tcp, host ip = , host port = 5432, guest ip = , guest port = 5432

In your Ubuntu guest, examine /etc/postgresql/8.4/main/postgresql.conf:

sudo nano /etc/postgresql/8.4/main/postgresql.conf

Look for the listen_addresses parameter. It should be configured as:

listen_addresses = 'localhost,10.0.2.15'  # or your guest IP

From your Windows host, try these diagnostic commands:

telnet 127.0.0.1 5432
psql -h 127.0.0.1 -p 5432 -U postgres

Your current pg_hba.conf configuration looks good, but let's make it more robust:

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
host    all         all         127.0.0.1/32          md5
host    all         all         10.0.2.0/24           md5
host    all         all         192.168.1.0/24        md5

Even with firewalls disabled, try these iptables commands in your guest:

sudo iptables -A INPUT -p tcp --dport 5432 -j ACCEPT
sudo iptables -L -n | grep 5432

If port forwarding still fails, consider these alternatives:

# Bridge networking configuration in VirtualBox
VBoxManage modifyvm "Your_VM_Name" --nic1 bridged --bridgeadapter1 "Your_Network_Adapter"

# Then connect directly via guest IP:
psql -h 192.168.1.x -p 5432 -U postgres