]> git.sur5r.net Git - bacula/docs/blob - docs/manuals/en/catalog/postgresql.tex
Add first cut reorganization
[bacula/docs] / docs / manuals / en / catalog / 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 {\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\_bacula\_database
122
123    This script creates the PostgreSQL {\bf bacula} database.  
124    Before running this command, you should carefully think about
125    what encoding sequence you want for the text fields (paths, files, ...).
126    Ideally, the encoding should be set to UTF8. However, many Unix systems
127    have filenames that are not encoded in UTF8, either because you have
128    not set UTF8 as your default character set or because you have imported
129    files from elsewhere (e.g. MacOS X).  For this reason, Bacula uses
130    SQL\_ASCII as the default encoding.  If you want to change this,
131    please modify the script before running it, but be forewarned that
132    Bacula backups will fail if PostgreSQL finds any non-UTF8 sequences.
133
134    If running the script fails, it is probably because the database is
135    owned by a user other than yourself.  On many systems, the database
136    owner is {\bf pgsql} and on others such as Red Hat and Fedora it is {\bf
137    postgres}.  You can find out which it is by examining your /etc/passwd
138    file.  To create a new user under either your name or with say the name
139    {\bf bacula}, you can do the following:
140
141 \begin{verbatim}
142    su
143    (enter root password)
144    su pgsql (or postgres)
145    createuser kern (or perhaps bacula)
146    Shall the new user be allowed to create databases? (y/n) y
147    Shall the new user be allowed to create more new users? (y/n) (choose
148          what you want)
149    exit
150 \end{verbatim}
151     
152     At this point, you should be able to execute the
153     ./create\_bacula\_database command.
154
155 \item ./make\_bacula\_tables
156
157    This script creates the PostgreSQL tables used by {\bf Bacula}.  
158 \item ./grant\_bacula\_privileges
159
160    This script creates the database user {\bf bacula}  with restricted access
161 rights. You may  want to modify it to suit your situation. Please note that 
162 this database is not password protected.  
163
164 \end{enumerate}
165
166 Each of the three scripts (create\_bacula\_database, make\_bacula\_tables, and
167 grant\_bacula\_privileges) allows the addition of a command line argument.
168 This can be useful for specifying the user name. For example, you might need
169 to add {\bf -h hostname} to the command line to specify a remote database
170 server. 
171
172 To take a closer look at the access privileges that you have setup with the
173 above, you can do: 
174
175 \footnotesize
176 \begin{verbatim}
177 PostgreSQL-directory/bin/psql --command \\dp bacula
178 \end{verbatim}
179 \normalsize
180
181 Also, I had an authorization problem with the password. In the end,
182 I had to modify my {\bf pg\_hba.conf} file (in /var/lib/pgsql/data on my machine)
183 from:
184
185 \footnotesize
186 \begin{verbatim}
187   local   all    all        ident  sameuser
188 to
189   local   all    all        trust  sameuser
190 \end{verbatim}
191 \normalsize
192
193 This solved the problem for me, but it is not always a good thing
194 to do from a security standpoint.  However, it allowed me to run
195 my regression scripts without having a password.
196
197 A more secure way to perform database authentication is with md5
198 password hashes.  Begin by editing the {\bf pg\_hba.conf} file, and
199 just prior the the existing ``local'' and ``host'' lines, add the line:
200
201 \footnotesize
202 \begin{verbatim}
203   local bacula bacula md5
204 \end{verbatim}
205 \normalsize
206
207 and restart the Postgres database server (frequently, this can be done
208 using "/etc/init.d/postgresql restart" or "service postgresql restart") to
209 put this new authentication rule into effect.
210
211 Next, become the Postgres administrator, postgres, either by logging
212 on as the postgres user, or by using su to become root and then using
213 su - postgres to become postgres.  Add a password to the bacula
214 database for the bacula user using:
215
216 \footnotesize
217 \begin{verbatim}
218   \$ psql bacula
219   bacula=# alter user bacula with password 'secret';
220   ALTER USER
221   bacula=# \\q
222 \end{verbatim}
223 \normalsize
224
225 You'll have to add this password to two locations in the
226 bacula-dir.conf file: once to the Catalog resource and once to the
227 RunBeforeJob entry in the BackupCatalog Job resource.  With the
228 password in place, these two lines should look something like:
229
230 \footnotesize
231 \begin{verbatim}
232   dbname = bacula; user = bacula; password = "secret"
233     ... and ...
234   # WARNING!!! Passing the password via the command line is insecure.
235   # see comments in make_catalog_backup for details.
236   RunBeforeJob = "/etc/make_catalog_backup bacula bacula secret"
237 \end{verbatim}
238 \normalsize
239
240 Naturally, you should choose your own significantly more random
241 password, and ensure that the bacula-dir.conf file containing this
242 password is readable only by the root.
243
244 Even with the files containing the database password properly
245 restricted, there is still a security problem with this approach: on
246 some platforms, the environment variable that is used to supply the
247 password to Postgres is available to all users of the
248 local system.  To eliminate this problem, the Postgres team have
249 deprecated the use of the environment variable password-passing
250 mechanism and recommend the use of a .pgpass file instead.  To use
251 this mechanism, create a file named .pgpass containing the single
252 line:
253
254 \footnotesize
255 \begin{verbatim}
256   localhost:5432:bacula:bacula:secret
257 \end{verbatim}
258 \normalsize
259
260 This file should be copied into the home directory of all accounts
261 that will need to gain access to the database: typically, root,
262 bacula, and any users who will make use of any of the console
263 programs.  The files must then have the owner and group set to match
264 the user (so root:root for the copy in ~root, and so on), and the mode
265 set to 600, limiting access to the owner of the file.
266
267 \section{Re-initializing the Catalog Database}
268 \index[general]{Database!Re-initializing the Catalog }
269 \index[general]{Re-initializing the Catalog Database }
270
271 After you have done some initial testing with {\bf Bacula}, you will probably
272 want to re-initialize the catalog database and throw away all the test Jobs
273 that you ran. To do so, you can do the following: 
274
275 \footnotesize
276 \begin{verbatim}
277   cd <install-directory>
278   ./drop_bacula_tables
279   ./make_bacula_tables
280   ./grant_bacula_privileges
281 \end{verbatim}
282 \normalsize
283
284 Please note that all information in the database will be lost and you will be
285 starting from scratch. If you have written on any Volumes, you must write an
286 end of file mark on the volume so that Bacula can reuse it. Do so with: 
287
288 \footnotesize
289 \begin{verbatim}
290    (stop Bacula or unmount the drive)
291    mt -f /dev/nst0 rewind
292    mt -f /dev/nst0 weof
293 \end{verbatim}
294 \normalsize
295
296 Where you should replace {\bf /dev/nst0} with the appropriate tape drive
297 device name for your machine. 
298
299 \section{Installing PostgreSQL from RPMs}
300 \index[general]{PostgreSQL!Installing from RPMs}
301 \index[general]{Installing PostgreSQL from RPMs}
302 If you are installing PostgreSQL from RPMs, you will need to install
303 both the PostgreSQL binaries and the client libraries.  The client
304 libraries are usually found in a devel package, so you must
305 install:
306
307 \footnotesize
308 \begin{verbatim}
309   postgresql
310   postgresql-devel
311   postgresql-server
312   postgresql-libs
313 \end{verbatim}
314 \normalsize
315
316 These will be similar with most other package managers too.  After
317 installing from rpms, you will still need to run the scripts that set up
318 the database and create the tables as described above.
319
320
321 \section{Converting from MySQL to PostgreSQL}
322 \index[general]{PostgreSQL!Converting from MySQL to }
323 \index[general]{Converting from MySQL to PostgreSQL }
324
325 The conversion procedure presented here was worked out by Norm Dressler
326 \lt{}ndressler at dinmar dot com\gt{} 
327
328 This process was tested using the following software versions: 
329
330 \begin{itemize}
331 \item Linux Mandrake 10/Kernel 2.4.22-10 SMP 
332 \item Mysql Ver 12.21 Distrib 4.0.15, for mandrake-linux-gnu (i586) 
333 \item PostgreSQL 7.3.4 
334 \item Bacula 1.34.5 
335    \end{itemize}
336
337 WARNING: Always as a precaution, take a complete backup of your databases
338 before proceeding with this process! 
339
340 \begin{enumerate}
341 \item Shutdown bacula (cd /etc/bacula;./bacula stop)  
342 \item Run the following command to dump your Mysql database:  
343
344    \footnotesize
345 \begin{verbatim}
346        mysqldump -f -t -n >bacula-backup.dmp
347     
348 \end{verbatim}
349 \normalsize
350
351 \item Make a backup of your /etc/bacula directory (but leave the  original in
352    place).  
353 \item Go to your Bacula source directory and rebuild it to include  PostgreSQL
354    support rather then Mysql support. Check the  config.log file for your
355    original configure command and replace  enable-mysql with enable-postgresql.  
356 \item Recompile Bacula with a make and if everything compiles  completely,
357    perform a make install.  
358 \item Shutdown Mysql. 
359 \item Start PostgreSQL on your system.  
360 \item Create a bacula user in Postgres with the createuser command.  Depending on
361    your Postgres install, you may have to SU to the  user who has privileges to
362    create a user.  
363 \item Verify your pg\_hba.conf file contains sufficient permissions to  allow
364    bacula to access the server. Mine has the following since  it's on a secure
365    network:  
366
367 \footnotesize
368 \begin{verbatim}
369 local all all trust
370                 
371 host all all 127.0.0.1 255.255.255.255 trust
372                 
373 NOTE: you should restart your postgres server if you
374       made changes
375       
376 \end{verbatim}
377 \normalsize
378
379 \item Change into the /etc/bacula directory and prepare the database  and
380    tables with the following commands:  
381
382 \footnotesize
383 \begin{verbatim}
384 ./create_postgresql_database
385                                 
386 ./make_postgresql_tables
387                                 
388 ./grant_postgresql_privileges
389        
390 \end{verbatim}
391 \normalsize
392
393 \item Verify you have access to the database:  
394
395    \footnotesize
396 \begin{verbatim}
397   
398 psql -Ubacula bacula
399       
400 \end{verbatim}
401 \normalsize
402
403 You should not get any errors.  
404 \item Load your database from the Mysql database dump with:  
405
406    \footnotesize
407 \begin{verbatim}
408 psql -Ubacula bacula <bacula-backup.dmp>
409       
410 \end{verbatim}
411 \normalsize
412
413 \item Resequence your tables with the following commands:  
414
415    \footnotesize
416 \begin{verbatim}
417 psql -Ubacula bacula
418                 
419 SELECT SETVAL('basefiles_baseid_seq', (SELECT
420 MAX(baseid) FROM basefiles));
421 SELECT SETVAL('client_clientid_seq', (SELECT
422 MAX(clientid) FROM client));
423 SELECT SETVAL('file_fileid_seq', (SELECT MAX(fileid)
424 FROM file));
425 SELECT SETVAL('filename_filenameid_seq', (SELECT
426 MAX(filenameid) FROM filename));
427                 
428 SELECT SETVAL('fileset_filesetid_seq', (SELECT
429 MAX(filesetid) FROM fileset));
430                 
431 SELECT SETVAL('job_jobid_seq', (SELECT MAX(jobid) FROM job));
432 SELECT SETVAL('jobmedia_jobmediaid_seq', (SELECT
433 MAX(jobmediaid) FROM jobmedia));
434 SELECT SETVAL('media_mediaid_seq', (SELECT MAX(mediaid) FROM media));
435 SELECT SETVAL('path_pathid_seq', (SELECT MAX(pathid) FROM path));
436                 
437 SELECT SETVAL('pool_poolid_seq', (SELECT MAX(poolid) FROM pool));
438        
439 \end{verbatim}
440 \normalsize
441
442 \item At this point, start up Bacula, verify your volume library and  perform
443    a test backup to make sure everything is working  properly. 
444 \end{enumerate}
445
446 \section{Upgrading PostgreSQL}
447 \index[general]{Upgrading PostgreSQL }
448 \index[general]{Upgrading!PostgreSQL }
449 \index[general]{Upgrading}
450 If you upgrade PostgreSQL, you must reconfigure, rebuild, and re-install 
451 Bacula otherwise you are likely to get bizarre failures.  If you
452 to modify the bacula.spec file to account for the new PostgreSQL version.
453 You can do so by rebuilding from the source rpm. To do so, you may need
454 install from rpms and you upgrade PostgreSQL, you must also rebuild Bacula.
455
456
457 \section{Credits}
458 \index[general]{Credits }
459 Many thanks to Dan Langille for writing the PostgreSQL driver. This will
460 surely become the most popular database that Bacula supports.