If like to keep the PC I use for development as clean as possible, to reduce the odds of “other installed stuff” influencing whatever I’m writing. Whenever I need to install some kind of server-type software on it, I prefer to use small virtual machines to install them in. Like a sandbox running the server, which I can just start and access from the host machine whenever I need it. Another big advantage of this approach is that it allows me to just copy the entire virtual machine to another machine and run the server there, for example on a colleague’s machine. Below are some tips on getting the open source database server PostgreSQL running on a virtual machine, accessible from the host (or any other machine on your network).
A good choice for quickly setting up a virtual machine is VirtualBox. It used to be Sun’s and since the recent acquisition of Sun by Oracle, it’s now Oracle VirtualBox. VirtualBox runs on Windows, Linux and OS X and you can install a variety of OSes on it. I run Windows 7 on my machine, but it runs several virtual boxes, including ones with Debian, FreeBSD, Ubuntu and an old copy of Windows XP. Remember that, even though the virtual machine is .. well, virtual, you still have to have a valid license for any OS you run on it. So, I’d recommend going with something free and simple, like an Ubuntu Minimal CD Image installation. You get some decent options to install during installation and beyond that, it’s just a nice, light Linux installation with a desktop. You just mount the iso as a disc in the virtual CDROM drive and boot the virtual machine.
Getting PostgreSQL up and running on the virtual machine works the same as doing it on a real machine, so I’ll assume that’s not a problem. The tricky part is getting to the PostgreSQL server on the guest from the host (and the rest of the network). To get that working, you need to follow these steps:
Now, you can just start up the virtual machine whenever you need a PostgreSQL server and just access it from the host as if it were running locally. No services installed on the host, no files cluttering your file system and if you need a PostgreSQL server elsewhere, or you just want to bring the whole thing over to another location (data and all), it’s a simple as moving the .vdi file.
One more tip: before installing anything else on a virtual machine, but after installing the OS, clone the VirtualBox. That way, if you decide to do things differently, or if you need more virtual machines, you don’t have to go through the OS setup process again.
Hello, great guide, I have noob question though 🙂 Can you connect through NAT the other way around ? PostreSQL running on Host and connect from Guest ?
Thanks, you’re welcome. You should be able to connect to a PostgreSQL instance running on the host from the guest fairly easily. Figure out the address of the host from inside the Virtualbox, either by running ‘ipconfig’ from a Windows guest, or ‘netstat -rn’ from a Linux guest.
The address you’re looking for is the gateway address for that machine. You should be able to connect to most services running on the host from within the guest on that ip. Too allow connections from the guest on the PostgreSQL server, the process is pretty much the same as with the inverse: check what IP your Virtualbox is on and make sure connections from that IP are allowed on the PostgreSQL server.
Pretty! This has been an incredibly wonderful post.
Thanks for supplying this info.