I’ve been using FreeNas for a couple of years now and each version up to now, I have jumped through a few hoops to get PostgreSQL working on it.
This week I saw that FreeNas 9.1 has moved from the -rc category to a distribution download. This version has a new jail/plugin architecture which looks very good and very flexible. The ability to have multiple jails makes a nice way to partition the processes and keep things from stomping on each other.
My system is set up with the main data storage set up as a ZFS RAID array. For the plugin/jail storage, i added a couple of disks in a mirror configuration. All user-data sits on the main data storage and is mounted in the appropriate directories in the jail(s).
I had placed the ~/data directory on the main data partition, so it was safe whilst the installation took place.
Installing PostgreSQL on FreeNas
Jail Creation
Create a new jail. Call it database (or pgsql etc). I created a portjail jail. I assigned an IP and unticked the vanila setting. Referring to the FreeNas documentation this meant that I can install both PC-BSD PBI files and FreeBSD packages, but not the FreeNas PBI pacakges.
Configuring the Jail
The next step needs to be done from the command line. Open a shell (I just connect via ssh) to the FreeNas server and navigate to the jails directory. (If you don’t know where this is, go to the GUI and open the Jails settings. In the Configuration tab, the first setting Jail Root will give you the location of the jail)
[root@freenas /mnt/volume1/jails]# ls -al total 8 drwxr-xr-x 20 root wheel 22 Aug 14 09:48 . drwxr-xr-x 6 root wheel 6 Aug 13 11:34 .. drwxr-xr-x 2 root wheel 16 Aug 13 13:25 .database.meta drwxr-xr-x 4 root wheel 4 Aug 13 11:48 .warden-files-cache drwxr-xr-x 17 root wheel 21 Dec 4 2012 .warden-template-9.1-RELEASE-amd64 drwxrwxr-x 17 1002 1002 22 Aug 13 11:49 .warden-template-9.1-RELEASE-amd64-pluginjail drwxrwxr-x 18 1002 1002 24 Aug 13 13:25 database -rw-r--r-- 1 root wheel 6794 Aug 14 09:48 warden.log
The directory .database.meta contains the information and settings for the jail
[root@freenas /mnt/volume1/jails]# cd .database.meta/ [root@freenas /mnt/volume1/jails/.database.meta]# l total 16 drwxr-xr-x 2 root wheel 16 Aug 13 13:25 . drwxr-xr-x 20 root wheel 22 Aug 14 09:48 .. -rw-r--r-- 1 root wheel 0 Aug 13 13:25 autostart -rw-r--r-- 1 root wheel 85 Aug 13 17:06 fstab -rw-r--r-- 1 root wheel 9 Aug 13 13:25 host -rw-r--r-- 1 root wheel 2 Aug 13 13:25 id -rw-r--r-- 1 root wheel 18 Aug 13 13:45 ipv4 -rw-r--r-- 1 root wheel 39 Aug 13 17:06 jail-flags -rw-r--r-- 1 root wheel 55 Aug 13 15:08 jail-portjail -rwxr-xr-x 1 root wheel 61 Aug 13 13:25 jail-post-delete -rwxr-xr-x 1 root wheel 60 Aug 13 13:25 jail-post-start -rwxr-xr-x 1 root wheel 59 Aug 13 13:25 jail-post-stop -rwxr-xr-x 1 root wheel 60 Aug 13 13:25 jail-pre-delete -rwxr-xr-x 1 root wheel 59 Aug 13 13:25 jail-pre-start -rwxr-xr-x 1 root wheel 58 Aug 13 13:25 jail-pre-stop -rw-r--r-- 1 root wheel 0 Aug 13 13:25 vnet [root@freenas /mnt/volume1/jails/.database.meta]# more jail-flags allow.raw_sockets=true allow.sysvipc=1 [root@freenas /mnt/volume1/jails/.database.meta]#
When you first do this, you will see that the jail-flags file only contains allow.raw_sockets=true. You must add the line
allow.sysvipc=1
Without this line, PostgreSQL will not start. The error message you will see is
FATAL: could not create shared memory segment: Function not implemented DETAIL: Failed system call was shmget(key=1, size=1187840, 03600)
Check the version of PostgreSQL
check the PG_VERSION file in the existing PostgresQL data directory to make sure that the correct version in installed.
use
pkg search postgres
to find the correct packages. You may want to install the -client and -contrib packages also (plus any other extensions/languages)
Install PostgreSQL
Use the pkg install to install all the required packages.
pkg install postgresql-plperl-9.0.13 postgresql-server-9.0.13 postgresql-contrib-9.0.13 postgresql-docs-9.0.13 pgtcl-postgresql90-2.0.0
Configuring the PostgreSQL settings
make sure that you add these lines to /etc/rc.conf
postgresql_enable="YES" postgresql_data="/usr/local/pgsql/data"
The second line points to the data.
You can test the install by running the init command. If this is a completely new installation, you must do this. If this is a new installation, then you can start to configure access to the database and create any databases you need.
Using an existing database
To use an existing database, go to the FreeNas GUI and select the jail you have just created.
Stop the jail.
Click on the storage button and link your existing database into the location given by the postgresql_data setting in the rc.conf file.
Start the jail.
You will now need to perform your usual tests on the database itself to make sure that the access is working etc. Because my addresses were all identical, my server was immediately available.