Postgresql in a Jail

I had some trouble with installing postgresql in a jail and I did not write this guide but found it helpful I kept the original post together

Originally written by VileSYN (Kernel_Killer) of Network Synapse. Original Article

Since the release of FreeBSD 7.0 many things have been changed to allow PostGres to run better than any other database under FreeBSD. To take advantage of this, I'll show how to setup PostGreSQL in FreeBSD jails. Of course, the same method is used to install on your main system as well.



First off, we need to compile our kernel with a few different options.

Code:

Code:
# Shared Memory
options         SHMMAXPGS=400000  # Maximum size of shared memory segment (in pages)
options         SHMSEC=512   # Maximum number of shared memory segments per process
options         SEMMNI=512    # Maximum number of semaphore identifiers
options         SEMMNS=1024    # Maximum number of semaphores system-wide
options         SEMUME=200    # Maximum number of undo entries per process
options         SEMMNU=512    # number of undo structures in system

You may notice later that after installing PostGreSQL, it will mention the same thing, but with lesser amounts. Using the amounts given by the port won't allow as many connections, and is optimized for a system with roughly around 256M of memory. The system I'm building this how-to around has over 2 gigabytes of RAM, and allocating 1.6 gigabytes for PostGreSQL in shared memory. If you are using less, you might want to divide SHMMAXPGS accordingly. The others, if you are using one gigabyte total, you might cut those in half. Keep in mind, this setup is fairly light on the server, considering it's not meant to be a full-fledged PostGreSQL server.

For use of shared memory for jails, be sure to add this to your /etc/sysctl.conf:

Code:

Code:
security.jail.sysvipc_allowed=1

Compile your new kernel, reboot, and then on to ports. If you want to check your allocated shared memory, you can check with:

# sysctl -a | grep shmmax

The settings above will show 'kern.ipc.shmmax: 1638400000'.

If you are planning to compile the database in a jail, be sure to mount the nullfs ports to the ports directory in the jail (refer to the FreeBSD 7.0 Jail How-To in order to see how this is done, if you have forgotten). Next, go to /usr/ports/databases/postgresql83-server, and do the usual, 'make install clean'. After it is done installing, edit your /etc/rc.conf, and add:

Code:
postgresql_enable="YES"

If you don't add this, the rc.d script will NOT work.

Next, you need to initialize the database. To do so, simply run:

# /usr/local/etc/rc.d/postgresql initdb

With this done, it's time to change the setup of the database. First, we go to /usr/local/pgsql/data, and edit the postgresql.conf. In here, we'll set the bind-address, port, and whatever else we need. If you plan to have other jails, or the base system access the database, set the bind-address to the IP of the jail itself.


So, that's it for this tutorial. You should now have a basic understanding of PostGreSQL, and be able to install it with full optimization in FreeBSD, and even in a jail if needed. Enjoy!

Copyright Network Synapse
 
Is it necessary to build a custom kernel to change those System V IPC settings? In the past I've configured semaphores in /boot/loader.conf.

Code:
kern.ipc.semmni=256
kern.ipc.semmns=512
kern.ipc.semmnu=256

The values I used are from a jail guide on FreeBSD Diary.
 
It's not necessary but I had some issues with /boot/loader.conf but I had to recompile my kernel anyways to add a feature I wanted to build in my kernel.
 
Back
Top