]> git.sur5r.net Git - bacula/docs/blob - docs/manuals/en/main/postgresql.tex
Tweak update copyrights
[bacula/docs] / docs / manuals / en / main / postgresql.tex
1 %%
2 %%
3
4 \chapter{Installing and Configuring PostgreSQL}
5 \label{PostgreSqlChapter}
6 \index[general]{PostgreSQL!Installing and Configuring }
7 \index[general]{Installing and Configuring PostgreSQL }
8 \index[general]{Upgrading}
9
10 If you have installed PostgreSQL from a package (.deb, .rpm, ...) or 
11 already installed it from source, please skip to the next section.
12
13 If you are building PostgreSQL from source, please be sure to add
14 the {\bf \verb:--:enable-thread-safety} option when doing the ./configure
15 for PostgreSQL.  
16
17 \section{Installing PostgreSQL}
18 \index[general]{PostgreSQL!Installing }
19
20 If you use the {\bf ./configure \verb:--:with-postgresql=PostgreSQL-Directory}
21 statement for configuring {\bf Bacula}, you will need PostgreSQL version 7.4
22 or later installed. NOTE! PostgreSQL versions earlier than 7.4 do not work
23 with Bacula. If PostgreSQL is installed in the standard system location, you
24 need only enter {\bf \verb:--:with-postgresql} since the configure program will
25 search all the standard locations. If you install PostgreSQL in your home
26 directory or some other non-standard directory, you will need to provide the
27 full path with the {\bf \verb:--:with-postgresql} option. 
28
29 Installing and configuring PostgreSQL is not difficult but can be confusing
30 the first time. If you prefer, you may want to use a package provided by your
31 chosen operating system. Binary packages are available on most PostgreSQL
32 mirrors. 
33
34 If you prefer to install from source, we recommend following the instructions
35 found in the 
36 \elink{PostgreSQL documentation}{http://www.postgresql.org/docs/}. 
37
38 If you are using FreeBSD, 
39 \elink{this FreeBSD Diary article}{http://www.freebsddiary.org/postgresql.php}
40 will be useful. Even if you are not using FreeBSD, the article will contain
41 useful configuration and setup information. 
42
43 If you configure the Batch Insert code in Bacula (attribute inserts are
44 10 times faster), you {\bf must} be using a PostgreSQL that was built with
45 the {\bf \verb:--:enable-thread-safety} option, otherwise you will get
46 data corruption. Most major Linux distros have thread safety turned on, but
47 it is better to check.  One way is to see if the PostgreSQL library that
48 Bacula will be linked against references pthreads.  This can be done
49 with a command such as:
50
51 \footnotesize
52 \begin{verbatim}
53   nm /usr/lib/libpq.a | grep pthread_mutex_lock
54 \end{verbatim}
55 \normalsize
56
57 The above command should print a line that looks like:
58
59 \footnotesize
60 \begin{verbatim}
61          U pthread_mutex_lock
62 \end{verbatim}
63 \normalsize
64
65 if does, then everything is OK. If it prints nothing, do not enable batch
66 inserts when building Bacula.
67
68 After installing PostgreSQL, you should return to completing the installation
69 of {\bf Bacula}. Later, after Bacula is installed, come back to this chapter
70 to complete the installation. Please note, the installation files used in the
71 second phase of the PostgreSQL installation are created during the Bacula
72 Installation. You must still come back to complete the second phase of the 
73 PostgreSQL installation even if you installed binaries (e.g. rpm, deb,
74 ...).
75
76
77 \label{PostgreSQL_configure}
78 \section{Configuring PostgreSQL}
79 \index[general]{PostgreSQL!Configuring PostgreSQL -- }
80
81 At this point, you should have built and installed PostgreSQL, or already have
82 a running PostgreSQL, and you should have configured, built and installed {\bf
83 Bacula}. If not, please complete these items before proceeding. 
84
85 Please note that the {\bf ./configure} used to build {\bf Bacula} will need to
86 include {\bf \verb:--:with-postgresql=PostgreSQL-directory}, where {\bf
87 PostgreSQL-directory} is the directory name that you specified on the
88 ./configure command for configuring PostgreSQL (if you didn't specify a
89 directory or PostgreSQL is installed in a default location, you do not need to
90 specify the directory). This is needed so that Bacula can find the necessary
91 include headers and library files for interfacing to PostgreSQL. 
92
93 An important thing to note here is that {\bf Bacula} makes two connections
94 to the PostgreSQL server for each backup job that is currently running.  If
95 you are intending to run a large number of concurrent jobs, check the value
96 of {\bf max\_connections} in your PostgreSQL configuration file to ensure
97 that it is larger than the setting {\bf Maximum Concurrent Jobs}
98 in your director configuration.  {\bf Setting this too low will result in
99 some backup jobs failing to run correctly!}
100
101 {\bf Bacula} will install scripts for manipulating the database (create,
102 delete, make tables etc) into the main installation directory. These files
103 will be of the form *\_bacula\_* (e.g. create\_bacula\_database). These files
104 are also available in the \lt{}bacula-src\gt{}/src/cats directory after
105 running ./configure. If you inspect create\_bacula\_database, you will see
106 that it calls create\_postgresql\_database. The *\_bacula\_* files are
107 provided for convenience. It doesn't matter what database you have chosen;
108 create\_bacula\_database will always create your database. 
109
110 Now you will create the Bacula PostgreSQL database and the tables that Bacula
111 uses. These instructions assume that you already have PostgreSQL running. You
112 will need to perform these steps as a user that is able to create new
113 databases. This can be the PostgreSQL user (on most systems, this is the pgsql
114 user). 
115
116 \begin{enumerate}
117 \item cd \lt{}install-directory\gt{}
118
119    This directory contains the Bacula catalog interface routines.  
120
121 \item Create the database owner ({\bf bacula})
122    On many systems, the PostreSQL master 
123    owner is {\bf pgsql} and on others such as Red Hat and Fedora it is {\bf
124    postgres}.  You can find out which it is by examining your /etc/passwd
125    file.  To create a new user under either your name or with say the name
126    {\bf bacula}, you can do the following:
127
128 \begin{verbatim}
129    su
130    (enter root password)
131    su pgsql (or postgres)
132    createuser bacula
133    Shall the new user be allowed to create databases? (y/n) y
134    Shall the new user be allowed to create more new users? (y/n) (choose
135          what you want)
136    exit
137 \end{verbatim}
138
139    In newer versions of PostgreSQL, it will not automatically ask
140    if the user {\bf bacula} should be able to create databases or
141    create new users. To do so you may need to use the {\bf -s}
142    option on the {\bf createuser} command.  Example:
143
144 \begin{verbatim}
145    createuser -s bacula
146 \end{verbatim}
147
148    Normally the {\bf bacula} user must be able to create new databases, if
149    you use the script in the next item, or you will have to create one for
150    it, but it does not need to create new users.
151
152
153 \item ./create\_bacula\_database
154
155    This script creates the PostgreSQL {\bf bacula} database.  
156    Before running this command, you should carefully think about
157    what encoding sequence you want for the text fields (paths, files, ...).
158    We strongly recommend that you use the default value of SQL\_ASCII
159    that is in the create\_bacula\_database script.  Please be warned
160    that if you change this value, your backups may fail.  After running
161    the script, you can check with the command:
162
163 \begin{verbatim}
164    psql -l
165 \end{verbatim}
166
167    and the column marked {\bf Encoding} should be {\bf SQL\_ASCII} for
168    all your Bacula databases (normally {\bf bacula}).
169
170 \item ./make\_bacula\_tables
171
172    This script creates the PostgreSQL tables used by {\bf Bacula}.  
173 \item ./grant\_bacula\_privileges
174
175    This script creates the database user {\bf bacula}  with restricted access
176 rights. You may  want to modify it to suit your situation. Please note that 
177 this database is not password protected.  
178
179 \end{enumerate}
180
181 Each of the three scripts (create\_bacula\_database, make\_bacula\_tables, and
182 grant\_bacula\_privileges) allows the addition of a command line argument.
183 This can be useful for specifying the user name. For example, you might need
184 to add {\bf -h hostname} to the command line to specify a remote database
185 server. 
186
187 To take a closer look at the access privileges that you have setup with the
188 above, you can do: 
189
190 \footnotesize
191 \begin{verbatim}
192 PostgreSQL-directory/bin/psql --command \\dp bacula
193 \end{verbatim}
194 \normalsize
195
196 Also, I had an authorization problem with the password. In the end,
197 I had to modify my {\bf pg\_hba.conf} file (in /var/lib/pgsql/data on my machine
198 in /var/lib/postgresql/8.x on others, and in /etc/postgres/8.x/main on
199 still others -- what a mess!) from:
200
201 \footnotesize
202 \begin{verbatim}
203   local   all    all        ident  sameuser
204 to
205   local   all    all        trust
206 \end{verbatim}
207 \normalsize
208
209 This solved the problem for me, but it is not always a good thing
210 to do from a security standpoint.  However, it allowed me to run
211 my regression scripts without having a password.
212
213 A more secure way to perform database authentication is with md5
214 password hashes.  Begin by editing the {\bf pg\_hba.conf} file, and
215 above the existing ``local'' and ``host'' lines, add the line:
216
217 \footnotesize
218 \begin{verbatim}
219   local bacula bacula md5
220 \end{verbatim}
221 \normalsize
222
223 then restart the Postgres database server (frequently, this can be done
224 using "/etc/init.d/postgresql restart" or "service postgresql restart") to
225 put this new authentication rule into effect.
226
227 Next, become the Postgres administrator, postgres, either by logging
228 on as the postgres user, or by using su to become root and then using
229 {\bf su - postgres} or {\bf su - pgsql} to become postgres.  
230 Add a password to the {\bf bacula} database for the {\bf bacula} user using:
231
232 \footnotesize
233 \begin{verbatim}
234   \$ psql bacula
235   bacula=# alter user bacula with password 'secret';
236   ALTER USER
237   bacula=# \\q
238 \end{verbatim}
239 \normalsize
240
241 You'll have to add this password to two locations in the
242 bacula-dir.conf file: once to the Catalog resource and once to the
243 RunBeforeJob entry in the BackupCatalog Job resource.  With the
244 password in place, these two lines should look something like:
245
246 \footnotesize
247 \begin{verbatim}
248   dbname = bacula; user = bacula; password = "secret"
249     ... and ...
250   # WARNING!!! Passing the password via the command line is insecure.
251   # see comments in make_catalog_backup for details.
252   RunBeforeJob = "/etc/make_catalog_backup bacula bacula secret"
253 \end{verbatim}
254 \normalsize
255
256 Naturally, you should choose your own significantly more random
257 password, and ensure that the bacula-dir.conf file containing this
258 password is readable only by the root.
259
260 Even with the files containing the database password properly
261 restricted, there is still a security problem with this approach: on
262 some platforms, the environment variable that is used to supply the
263 password to Postgres is available to all users of the
264 local system.  To eliminate this problem, the Postgres team have
265 deprecated the use of the environment variable password-passing
266 mechanism and recommend the use of a .pgpass file instead.  To use
267 this mechanism, create a file named .pgpass containing the single
268 line:
269
270 \footnotesize
271 \begin{verbatim}
272   localhost:5432:bacula:bacula:secret
273 \end{verbatim}
274 \normalsize
275
276 This file should be copied into the home directory of all accounts
277 that will need to gain access to the database: typically, root,
278 bacula, and any users who will make use of any of the console
279 programs.  The files must then have the owner and group set to match
280 the user (so root:root for the copy in root, and so on), and the mode
281 set to 600, limiting access to the owner of the file.
282
283 \section{Re-initializing the Catalog Database}
284 \index[general]{Database!Re-initializing the Catalog }
285 \index[general]{Re-initializing the Catalog Database }
286
287 After you have done some initial testing with {\bf Bacula}, you will probably
288 want to re-initialize the catalog database and throw away all the test Jobs
289 that you ran. To do so, you can do the following: 
290
291 \footnotesize
292 \begin{verbatim}
293   cd <install-directory>
294   ./drop_bacula_tables
295   ./make_bacula_tables
296   ./grant_bacula_privileges
297 \end{verbatim}
298 \normalsize
299
300 Please note that all information in the database will be lost and you will be
301 starting from scratch. If you have written on any Volumes, you must write an
302 end of file mark on the volume so that Bacula can reuse it. Do so with: 
303
304 \footnotesize
305 \begin{verbatim}
306    (stop Bacula or unmount the drive)
307    mt -f /dev/nst0 rewind
308    mt -f /dev/nst0 weof
309 \end{verbatim}
310 \normalsize
311
312 Where you should replace {\bf /dev/nst0} with the appropriate tape drive
313 device name for your machine. 
314
315 \section{Installing PostgreSQL from RPMs}
316 \index[general]{PostgreSQL!Installing from RPMs}
317 \index[general]{Installing PostgreSQL from RPMs}
318 If you are installing PostgreSQL from RPMs, you will need to install
319 both the PostgreSQL binaries and the client libraries.  The client
320 libraries are usually found in a {\bf devel} or {\bf dev} package, so you must
321 install the following for rpms:
322
323 \footnotesize
324 \begin{verbatim}
325   postgresql
326   postgresql-devel
327   postgresql-server
328   postgresql-libs
329 \end{verbatim}
330 \normalsize
331
332
333 and the following for debs:
334
335 \footnotesize
336 \begin{verbatim}
337   postgresql
338   postgresql-common
339   postgresql-client
340   postgresql-client-common
341   libpq5
342   libpq-dev
343 \end{verbatim}
344 \normalsize
345
346
347 These will be similar with most other package managers too.  After
348 installing from rpms, you will still need to run the scripts that set up
349 the database and create the tables as described above.
350
351
352 \section{Converting from MySQL to PostgreSQL}
353 \index[general]{PostgreSQL!Converting from MySQL to }
354 \index[general]{Converting from MySQL to PostgreSQL }
355
356 The conversion procedure presented here was worked out by Norm Dressler
357 \lt{}ndressler at dinmar dot com\gt{} 
358
359 This process was tested using the following software versions: 
360
361 \begin{itemize}
362 \item Linux Ubuntu Lucid
363 \item Mysql Ver 5.0.83
364 \item PostgreSQL 8.4.4
365 \item Bacula 5.0
366    \end{itemize}
367
368 WARNING: Always as a precaution, take a complete backup of your databases
369 before proceeding with this process! 
370
371 \begin{enumerate}
372 \item Shutdown bacula (cd /etc/bacula;./bacula stop)  
373 \item Run the following command to dump your Mysql database:  
374
375    \footnotesize
376 \begin{verbatim}
377 mysqldump -t -n -c --compatible=postgresql --skip-quote-names --skip-opt \
378           --disable-keys --lock-tables -u bacula -ppassword bacula \
379   | grep -v "INSERT INTO Status" \
380   | sed -e 's/0000-00-00 00:00:00/1970-01-01 00:00:00/g' \
381   | sed -e 's/\\0//' > bacula-backup.sql    
382 \end{verbatim}
383 \normalsize
384
385 \item Make a backup of your /etc/bacula directory (but leave the  original in
386    place).  
387 \item Go to your Bacula source directory and rebuild it to include  PostgreSQL
388    support rather then Mysql support. Check the  config.log file for your
389    original configure command and replace  enable-mysql with enable-postgresql.  
390 \item Recompile Bacula with a make and if everything compiles  completely,
391    perform a make install.  
392 \item Shutdown Mysql. 
393 \item Start PostgreSQL on your system.  
394 \item Create a bacula user in Postgres with the createuser command.  Depending on
395    your Postgres install, you may have to SU to the  user who has privileges to
396    create a user, you can also have to change permissions on catalog scripts
397    to fit your situation.  
398 \item Verify your pg\_hba.conf file contains sufficient permissions to  allow
399    bacula to access the server. Mine has the following since  it's on a secure
400    network:  
401
402 \footnotesize
403 \begin{verbatim}
404 local all all trust
405                 
406 host all all 127.0.0.1 255.255.255.255 trust
407                 
408 NOTE: you should reload (or restart) your postgres server if you  made changes
409       
410 \end{verbatim}
411 \normalsize
412
413 \item Change into the /etc/bacula directory and prepare the database  and
414    tables with the following commands:  
415
416 \footnotesize
417 \begin{verbatim}
418 ./create_postgresql_database
419                                 
420 ./make_postgresql_tables
421                                 
422 ./grant_postgresql_privileges
423        
424 \end{verbatim}
425 \normalsize
426
427 \item Verify you have access to the database:  
428
429    \footnotesize
430 \begin{verbatim}
431   
432 psql -Ubacula bacula
433       
434 \end{verbatim}
435 \normalsize
436
437 You should not get any errors.  
438 \item Load your database from the Mysql database dump with:  
439
440    \footnotesize
441 \begin{verbatim}
442 psql -Ubacula bacula <bacula-backup.dmp>
443       
444 \end{verbatim}
445 \normalsize
446
447 \item Resequence your tables with the following commands:  
448
449    \footnotesize
450 \begin{verbatim}
451 psql -Ubacula bacula
452                 
453 SELECT SETVAL('basefiles_baseid_seq', (SELECT MAX(baseid) FROM basefiles));
454 SELECT SETVAL('client_clientid_seq', (SELECT MAX(clientid) FROM client));
455 SELECT SETVAL('file_fileid_seq', (SELECT MAX(fileid) FROM file));
456 SELECT SETVAL('filename_filenameid_seq', (SELECT MAX(filenameid) FROM filename));
457 SELECT SETVAL('fileset_filesetid_seq', (SELECT MAX(filesetid) FROM fileset));
458 SELECT SETVAL('job_jobid_seq', (SELECT MAX(jobid) FROM job));
459 SELECT SETVAL('jobmedia_jobmediaid_seq', (SELECT MAX(jobmediaid) FROM jobmedia));
460 SELECT SETVAL('media_mediaid_seq', (SELECT MAX(mediaid) FROM media));
461 SELECT SETVAL('path_pathid_seq', (SELECT MAX(pathid) FROM path));
462 SELECT SETVAL('basefiles_baseid_seq', (SELECT MAX(baseid) FROM basefiles));
463 SELECT SETVAL('client_clientid_seq', (SELECT MAX(clientid) FROM client));
464 SELECT SETVAL('file_fileid_seq', (SELECT MAX(fileid) FROM file));
465 SELECT SETVAL('filename_filenameid_seq', (SELECT MAX(filenameid) FROM filename));
466 SELECT SETVAL('fileset_filesetid_seq', (SELECT MAX(filesetid) FROM fileset));
467 SELECT SETVAL('job_jobid_seq', (SELECT MAX(jobid) FROM job));
468 SELECT SETVAL('jobmedia_jobmediaid_seq', (SELECT MAX(jobmediaid) FROM jobmedia));
469 SELECT SETVAL('media_mediaid_seq', (SELECT MAX(mediaid) FROM media));
470 SELECT SETVAL('path_pathid_seq', (SELECT MAX(pathid) FROM path));
471 SELECT SETVAL('pool_poolid_seq', (SELECT MAX(poolid) FROM pool));
472
473 SELECT SETVAL('device_deviceid_seq', (SELECT MAX(deviceid) FROM device));
474 SELECT SETVAL('location_locationid_seq', (SELECT MAX(locationid) FROM location));
475 SELECT SETVAL('locationlog_loclogid_seq', (SELECT MAX(loclogid) FROM locationlog));
476 SELECT SETVAL('log_logid_seq', (SELECT MAX(logid) FROM log));
477 SELECT SETVAL('mediatype_mediatypeid_seq', (SELECT MAX(mediatypeid) FROM mediatype));
478 SELECT SETVAL('storage_storageid_seq', (SELECT MAX(storageid) FROM storage));       
479 \end{verbatim}
480 \normalsize
481
482 \item At this point, start up Bacula, verify your volume library and  perform
483    a test backup to make sure everything is working  properly. 
484 \end{enumerate}
485
486 \section{Upgrading PostgreSQL}
487 \index[general]{Upgrading PostgreSQL }
488 \index[general]{Upgrading!PostgreSQL }
489 \index[general]{Upgrading}
490 If you upgrade PostgreSQL, you must reconfigure, rebuild, and re-install 
491 Bacula otherwise you are likely to get bizarre failures.  If you
492 to modify the bacula.spec file to account for the new PostgreSQL version.
493 You can do so by rebuilding from the source rpm. To do so, you may need
494 install from rpms and you upgrade PostgreSQL, you must also rebuild Bacula.
495
496 \section{Tuning PostgreSQL}
497 \index[general]{Tuning} 
498
499 If you despool attributes for many jobs at the same time, you can tune the
500 sequence object for the \texttt{FileId} field.
501 \begin{verbatim}
502 psql -Ubacula bacula
503
504 ALTER SEQUENCE file_fileid_seq CACHE 1000;
505 \end{verbatim}
506
507 \section{Credits}
508 \index[general]{Credits }
509 Many thanks to Dan Langille for writing the PostgreSQL driver. This will
510 surely become the most popular database that Bacula supports.