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