From a .NET App inside a VM to a Postgres instance on the OSX host

While trying out persistence strategies for a .NET app I am mostly developing inside a VM, I was interested in looking at alternatives to RavenDB. Trouble is that RavenDB is not that lightweight in terms of startup, and I can’t get it to run outside the VM.

Many cool kids are playing with Postgres now - It may be that MySQL is simply a dead horse, or that Postgres’ capabilities in storing and retrieving JSON can be on a par with document databases under certain circumstances.

Hence I would like to give Postgres a spin, with the thought that the DB would be running on OSX while I develop the remaining parts in the VM.

Install Postgres

Being a newbie, I went for the install of a *.app, a very, very simple way to get started with Postgres on OSX. It starts up very quick, and you’ve got something running in a minute.

You can start talking to the instance with the CLI psql, but with my SQL-foo currently being a bit rusty, I wanted a little bit of GUI. I settled for pgadmin.

No rights for nobody

My Postgres instance would only want to talk to things running on the same machine. In order for the VM to be able to talk to the instance, the DB must be configured appropriately - this required modifications in configuration files.

Where those are can be asked e.g. via psql

SHOW config_file;

Which hopefully gives you a path where the config is located (mine was ~/Library/Application Support/Postgres/var-9.4/postgresql.conf).

Here I changed postgresql.conf to contain the line

listen_addresses = ’*’

When it is missing, te default is localhost.

The file pg_hba.conf was modified to contain a line with regard to the ability who can connect how from where to what.

host all all 0.0.0.0/0 trust

This is certainly not a setup for a production machine, obviously. The documentation of the configs are pretty good so it should give you enough hints to restrict access far more intelligently than the wildcard I essentially defined.

Finally I created a user with the GUI and granted that user connect right to the DB I want to play with.

GRANT CONNECT ON DATABASE db TO user;

On the VMWare I downloaded the Nugets Dapper and Npgsql.

And finally…

var b = new NpgsqlConnectionStringBuilder();
b.UserName = "user";
b.Password = "pwd";
b.Database = "myDB";
b.Host = "Franks-MBP";
b.Port = 5432;
var c = new NpgsqlConnection(b);
c.Open();
c.Close();

And the test is green.