Running PostgreSQL in a FreeBSD Jail

In the past we had to use the old SysV IPC sysctls and change the UID that each PostgreSQL server ran as under in each Jail. Which was annoying and error prone.

I started down this path because I was trying to figure out which tweaks are needed to allow PostgreSQL to run. Initially I was seeing the service postgresql initdb error out with this error:

creating template1 database in /usr/local/pgsql/data/base/1 ... FATAL: could not create shared memory segment: Function not implemented

In digging into this problem and the fact that PostgreSQL should be able to use posix shared memory now, I discovered some new jail knobs. In FreeBSD 11.0 and later it is very easy to run PostgreSQL in a FreeBSD jail.

In your jail.conf, just add:

sysvmsg=new
sysvsem
sysvshm

Or if like me you are using ezjail, just add to config file for that specific jail on the _parameters line, like so:

export jail_mon_example_com_parameters="sysvmsg=new sysvsem sysvshm"

Then start the jail and install PostgreSQL:

pkg install postgresql95-server

Verify that we can initialize the database correctly:

service postgresql initdb

If that completes without error, as it should, go ahead and start up the PostgreSQL service:

service postgresql start

I really like this method as it keeps the database service isolated with the service it is running for and keeps the shared memory and the shared memory config isolated into that one jail.