
#include "bacula_template.inc" title="Installing and Configuring PostgreSQL" num=24

<H1>Installing and Configuring PostgreSQL</H1>

<h2>Installing and Configuring PostgreSQL -- Phase I</h2>
If you use the <b>./configure --with-postgresql=PostgreSQL-Directory</b> statement
for configuring <b>Bacula</b>, you will need PostgreSQL version 7.3
or later installed. NOTE! PostgreSQL versions earlier than 7.3 do
not work with Bacula.
If PostgreSQL is 
installed in the standard system location, you need only enter
<b>--with-postgresql</b> since the configure program will search 
all the standard locations. If you install PostgreSQL in your home
directory or some other non-standard directory, you will need
to provide the full path with the <b>--with-postgresql</b> option.

<p>Installing and configuring PostgreSQL is not difficult but can
be confusing the first time.  If you prefer, you may want to 
use a package provided by your chosen operating system.  Binary 
packages are available on most PostgreSQL mirrors.

<p>
If you prefer to install from source, we recommend following the
instructions found in the
<a href="http://www.postgresql.org/docs/">PostgreSQL documentation</a>.

<p>
If you are using FreeBSD,
<a href="http://www.freebsddiary.org/postgresql.php">this FreeBSD
Diary article</a> will be useful.  Even if you are not using
FreeBSD, the article will contain useful configuration and setup
information.

<p>
After installing PostgreSQL, you should return to completing the installation
of <b>Bacula</b>. Later, after Bacula is installed, come
back to this chapter to complete the installation. Please note,
the installation files used in the second phase of the PostgreSQL 
installation are created during the Bacula Installation.

<a name="PostgreSQL_phase2"></a>
<h2>Installing and Configuring PostgreSQL -- Phase II</h2>
At this point, you should have built and installed PostgreSQL, or
already have a running PostgreSQL, and you should have configured,
built and installed <b>Bacula</b>. If not, please complete these
items before proceeding.

<p>Please note that the <b>./configure</b> used to build
<b>Bacula</b> will need to include
<b>--with-postgresql=PostgreSQL-directory</b>, where
<b>PostgreSQL-directory</b> is the directory name that you
specified on the ./configure command for configuring PostgreSQL
(if you didn't specify a directory or PostgreSQL is installed in
a default location, you do not need to specify the directory).
This is needed so that Bacula can find the necessary include
headers and library files for interfacing to PostgreSQL.

<p>
<b>Bacula</b> will install scripts for manipulating the database
(create, delete, make tables etc) into the main installation
directory.  These files will be of the form *_bacula_* (e.g.
create_bacula_database).  These files are also available in the
&lt;bacula-src&gt;/src/cats directory after running
./configure.  If you inspect create_bacula_database, you will see that it calls
create_postgresql_database.  The *_bacula_* files are provided for convenience.
It doesn't matter what database you have chosen; create_bacula_database will
always create your database.

<p>
Now you will create the Bacula PostgreSQL database and the
tables that Bacula uses.  These instructions assume that
you already have PostgreSQL running.  You will need to perform
these steps as a user that is able to create new databases.
This can be the PostgreSQL user (on most systems, this is the 
pgsql user).

<p>
<ol type="1">
  <li>cd &lt;install-directory&gt;<p></p>
     This directory contains the Bacula catalog 
     interface routines.</li>
     <p>
  <li>./create_bacula_database<p></p>
     This script creates the PostgreSQL <b>bacula</b> database.</li>
     <p>
  <li>./make_bacula_tables<p></p>
     This script creates the PostgreSQL tables used by <b>Bacula</b>.</li>
  <li>./grant_bacula_privileges<p></p>
     This script creates the database user <b>bacula</b>
     with restricted access rights. You may
     want to modify it to suit your situation. Please note that
     this database is not password protected.</li>
     <p>
</ol>
<p></p>
Each of the three scripts (create_bacula_database, make_bacula_tables,
and grant_bacula_privileges) allows the addition of a command line
argument. This can be useful for specifying the user name.
For example, you might need to add <b>-h hostname</b>
to the command line to specify a remote database server.
<p>
To take a closer look at the access privileges that you have
setup with the above, you can do:
<pre>
PostgreSQL-directory/bin/psql --command \\dp bacula
</pre>

<h2>Re-initializing the Catalog Database</h2>
After you have done some initial testing with <b>Bacula</b>, 
you will probably want to re-initialize the catalog database
and throw away all the test Jobs that you ran.
To do so, you can do the following:
<pre>
  cd &lt;install-directory&gt;
  ./drop_bacula_tables
  ./make_bacula_tables
  ./grant_bacula_privileges
</pre>
Please note that all information in the database will be
lost and you will be starting from scratch. If you have
written on any Volumes, you must write and end of file
mark on the volume so that Bacula can reuse it.
Do so with:
<pre>
   (stop Bacula or unmount the drive)
   mt -f /dev/nst0 rewind
   mt -f /dev/nst0 weof
</pre>

Where you should replace <b>/dev/nst0</b> with the
appropriate tape drive device name for your machine.

<h2>Converting from MySQL to PostgreSQL</h2>
The conversion procedure presented here was worked out by
Norm Dressler &lt;ndressler at dinmar dot com&gt;
<p>
This process was tested using the following software versions:

<ul>
 <li>Linux Mandrake 10/Kernel 2.4.22-10 SMP</li>
 <li>Mysql Ver 12.21 Distrib 4.0.15, for mandrake-linux-gnu (i586)</li>
 <li>PostgreSQL 7.3.4</li>
 <li>Bacula 1.34.5</li>
</ul>
        
WARNING: Always as a precaution, take a complete backup of your
databases before proceeding with this process!

<ol>
  <li>Shutdown bacula (cd /etc/bacula;./bacula stop)</li>
  <li>Run the following command to dump your Mysql database:
    <pre>
       mysqldump -f -t -n &gt;bacula-backup.dmp
    </pre>
    </li>
  <li>Make a backup of your /etc/bacula directory (but leave the
        original in place).</li>
  <li>Go to your Bacula source directory and rebuild it to include
        PostgreSQL support rather then Mysql support. Check the
        config.log file for your original configure command and replace
        enable-mysql with enable-postgresql.</li>
  <li>Recompile Bacula with a make and if everything compiles
        completely, perform a make install.</li>
  <li>Shutdown Mysql. </li>
  <li>Start PostgreSQL on your system.</li>
  <li>Create a bacula user in Postgres with createuser command.
        Depending on your Postgres install, you may have to SU to the
        user who has privileges to create a user.</li>
  <li>Verify your pg_hba.conf file contains sufficient permissions to
        allow bacula to access the server. Mine has the following since
        it's on a secure network:
    <pre>
local all all trust
                
host all all 127.0.0.1 255.255.255.255 trust
                
NOTE: you should restart your postgres server if you
      made changes
      </pre>             
      </li>
    <li>Change into the /etc/bacula directory and prepare the database
        and tables with the following commands:
       <pre>
./create_postgresql_database
                                
./make_postgresql_tables
                                
./grant_postgresql_privileges
       </pre>
      </li>
    <li>Verify you have access to the database:
      <pre>  
psql -Ubacula bacula
      </pre>
                                
You should not get any errors.
      </li>
    <li>Load your database from the Mysql database dump with:
      <pre>
psql -Ubacula bacula <bacula-backup.dmp
      </pre>
      </li>
    <li>Reseqence your tables with the following commands:
      <pre>
psql -Ubacula bacula
                
SELECT SETVAL('basefiles_baseid_seq', (SELECT
MAX(baseid) FROM basefiles));

SELECT SETVAL('client_clientid_seq', (SELECT
MAX(clientid) FROM client));

SELECT SETVAL('file_fileid_seq', (SELECT MAX(fileid)
FROM file));

SELECT SETVAL('filename_filenameid_seq', (SELECT
MAX(filenameid) FROM filename));
                
SELECT SETVAL('fileset_filesetid_seq', (SELECT
MAX(filesetid) FROM fileset));
                
SELECT SETVAL('job_jobid_seq', (SELECT MAX(jobid) FROM job));

SELECT SETVAL('jobmedia_jobmediaid_seq', (SELECT
MAX(jobmediaid) FROM jobmedia));

SELECT SETVAL('media_mediaid_seq', (SELECT MAX(mediaid) FROM media));

SELECT SETVAL('path_pathid_seq', (SELECT MAX(pathid) FROM path));
                
SELECT SETVAL('pool_poolid_seq', (SELECT MAX(poolid) FROM pool));
       </pre>
      </li>
    <li>At this point, start up Bacula, verify your volume library and
        perform a test backup to make sure everything is working
        properly.</li>
        
                        

<h2>Credits</h2>
Many thanks to Dan Languille for writing the PostgreSQL driver. This 
will surely become the most popular database that Bacula supports.

<page_prev name="Installing and Configuring MySQL" url="mysql.html">
<page_next name="Installing and Configuring SQLite" url="sqlite.html">
