How to Access to PostgreSQL in VirtualBox Guest OS from Host OS


Here’s a tips to connect PostgreSQL in VirtualBox guest OS, from host OS. Host OS can be Windows and Mac, because this is change of PostgreSQL setting.

Environment

I installed VirtualBox in host OS, and installed Ubuntu as guest os. After that, I installed PostgreSQL server into guest Ubuntu.

Host

I tried in Mac and Windows

  • Windows 10
  • macOS High Sierra 10.13.3

Guest

  • Ubuntu 17.10
  • PostgreSQL 9.5

The guest OS was connected with NAT system, and I configured port forwarding. If you have guest os connected through NAT, it is needed to build port forwarding connecting to the guest os PostgreSQL, TCP 5432.

Example of Port Forwarding

Change Settings

To connect PostgreSQL in the guest OS from the host OS, you have to change PostgreSQL configuration.

Listen Addresses

In /etc/postgresql/9.5/main/postresql.conf, there’s listen_addresses description. At first, it is commented out. Set '*' to listen_addresses.

PostgreSQL listens access from clients of listen addresses. As default, PostgreSQL listens access from localhost. PostgreSQL document is as follows.

Specifies the TCP/IP address(es) on which the server is to listen for connections from client applications. The value takes the form of a comma-separated list of host names and/or numeric IP addresses. The special entry * corresponds to all available IP interfaces. The entry 0.0.0.0 allows listening for all IPv4 addresses and :: allows listening for all IPv6 addresses. If the list is empty, the server does not listen on any IP interface at all, in which case only Unix-domain sockets can be used to connect to it. The default value is localhost, which allows only local TCP/IP “loopback” connections to be made. While client authentication (Chapter 20) allows fine-grained control over who can access the server, listen_addresses controls which interfaces accept connection attempts, which can help prevent repeated malicious connection requests on insecure network interfaces. This parameter can only be set at server start.

Authentication Configuration

/etc/postgresql/9.5/main/pg_hba.conf contains authentication information for clients. Add the following line into IPv4 section.