Search code examples
djangopostgresqlvirtualboxvagrantfile

Trouble Connecting to PostgreSQL Running in a Ubuntu VM


I have created an instance of PostgreSQL running in a Ubuntu/Bionic box in Vagrant/VirtualBox that will be used by Django in my dev environment. I wanted to test my ability to connect to it with either the terminal or pgAdmin before connecting with DJango, just to be sure it was working on that end first; the idea being that I could make later Django debugging easier if I am assured the connection works; but, I've had no success.

I have tried editing the configuration files that many posts suggest, with no effect. I can, however, ping the box via the ip assigned in the Vagrantfile with no issue - but not when specifying port 5432 with ping 10.1.1.1:5432. I can also use psql from within the box, so it's running.

I have made sure to enable ufw on the vm, created a rule to allow port 5432 and insured that it took using sudo ufw status. I have also confirmed that I'm editing the correct files using the show command within psql.

Here are the relevant configs as they currently are:

Vagrantfile:

Vagrant.configure("2") do |config|

  config.vm.hostname = "hg-site-db"

  config.vm.provider "virtualbox" do |v|

    v.memory = 2048
    v.cpus = 1
  end

  config.vm.box = "ubuntu/bionic64"

  config.vm.network "forwarded_port", host_ip: "127.0.0.1", guest: 5432, host: 5432

   config.vm.network "public_network", ip: "10.1.1.1"

  config.vm.provision "shell", inline: <<-SHELL
    # Update and upgrade the server packages.
    sudo apt-get update
    sudo apt-get -y upgrade

    # Install PostgreSQL
    sudo apt-get install -y postgresql postgresql-contrib   

    # Set Ubuntu Language
    sudo locale-gen en_US.UTF-8



  SHELL

end

/etc/postgresql/10/main/postgresql.conf:

listen_addresses = '*'

/etc/postgresql/10/main/pg_hba.conf - I am aware this is insecure, but I was just trying to find out why it was not working, with plans to go back and correct this:

host    all             all             0.0.0.0/0               trust

Solution

  • As we discussed in comments, you should remove host_ip from your forwarded port definition and just leave the guest and host ports.