Actually this is quite simple. All you have to do is specify a path when creating a database cluster. This comes from the postgresql documentation. The command to create a new pg cluster is initdb and looks like this:
initdb [option...] [--pgdata | -D] directory
According to the documentation,initdb
will create the specified directory unless it has no privileges to write to the target location. To be safe you can create it as root and then change ownership to postgres system user. In my system (used for development) I created a directory under /home
(on a raid1 array) that belongs to postgres
and ran the initdb
against that directory.
cd /home
sudo mkdir postgres
sudo chown postgres:postgres postgres
initdb [your db creation options here] --pgdata /home/postgres
Now you can start the postgresql server specifying the pgdata directory. For example,
pg_ctl start -D /home/postgres
will start a server instance that makes uses of your specified directory. If a server instance is already running you may wish to replace start
with restart
. This will bring down the running server and start it with the new options.
On a side note, as you may have already guessed, it is possible to have multiple instances of the server running at the same time (on different ports of course), each managing a separate cluster (ie physical path).