4 \chapter{PostgreSQL Installation und Konfiguration}
5 \label{PostgreSqlChapter}
6 \index[general]{PostgreSQL!Installation und Konfiguration }
7 \index[general]{PostgreSQL Installation und Konfiguration }
8 \index[general]{Update}
10 Wenn Sie sich dazu entschlie{\ss}en PostgreSQL zu verwenden,
11 sollten Sie sich \"{u}ber den Aufwand, den ein Datenbank-Update
12 mit sich bringt, im Klaren sein. Grunds\"{a}tzlich werden Sie bei
13 jeder neuen Hauptversion von PostgreSQL Ihre alte Datenbank
14 exportieren m\"{u}ssen, um sie dann in die neue Version einzupflegen.
15 Das wird dadurch erforderlich, dass sich regelm\"{a}{\ss}ig
16 einige internen "`Datenformate"' \"{a}ndern und von den
17 PostgreSQL-Entwicklern keine Tools zu Verf\"{u}gung gestellt
18 werden, um den Update-Vorgang zu automatisieren. Falls Sie den
19 Daten-Ex- und -Import vergessen sollten, kann es sein das Sie
20 auf die Datenbank nicht mehr zugreifen k\"{o}nnen, da die neue
21 PostgreSQL-Version nicht mit den alten Datenbank-Dateien
24 Sollten Sie PostgreSQL aus dem Quelltext selbst
25 kompilieren, m\"{u}ssen Sie dem configure-Kommando die
26 Option {\bf \verb:--:enable-thread-safety} \"{u}bergeben.
28 \section{PostgreSQL Installation}
29 \index[general]{PostgreSQL!Installation }
30 Wenn Sie bei der Konfiguration des Bacula-Quelltextes{\bf ./configure
31 \verb:--:with-postgresql=PostgreSQL-Verzeichnis} angeben, m\"{u}ssen Sie
32 PostgreSQL mindestens in Version 7.4 installiert haben. \"{A}ltere
33 PostgreSQL-Versionen finktionieren mit bacula nicht. Falls PostgreSQL
34 in einem Standard-Verzeichnis installiert ist, brauchen Sie das
35 "`PostgreSQL-Verzeichnis"' nicht angeben. Wenn es aber zum Beispiel in
36 Ihrem Home-Verzeichnis installiert ist, m\"{u}ssen Sie den kompletten
39 Die Konfiguration und Installation von PostgreSQL ist zwar nicht sehr
40 schwer, kann aber beim ersten Mal etwas verwirrend sein. Wenn Sie es vorziehen,
41 k\"{o}nnen Sie PostgreSQL auch \"{u}ber die Paket-Verwaltung Ihres Betriebssystems
42 installieren. Vorkompilierte Pakete finden Sie auf www.postgresql.org
43 unter \elink{Downloads}{http://www.postgresql.org/download/}
45 Wenn Sie PostgreSQL lieber aus dem Quelltext selbst kompilieren m\"{o}chten,
46 empfehlen wir Ihnen den Anweisungen in der \elink{PostgreSQL
47 Dokumentation}{http://www.postgresql.org/docs/} zu folgen.
49 Benutzer von FreeBSD finden in diesem \elink{FreeBSD Diary
50 Artikel}{http://www.freebsddiary.org/postgresql.php} weitere n\"{u}tzliche
51 Informationen. Selbstverst\"{a}ndlich enth\"{a}lt der Artikel auch f\"{u}r
52 Nicht-FreeBSD-Benutzer wissenswertes bez\"{u}glich der Installation und
53 Konfiguration von PostgreSQL
55 Falls Sie die Bacula "`Batch-Insert"'-Funktion benutzen wollen, die
56 standardm\"{a}{\ss}ig aktiviert ist und f\"{u}r eine schnelle Verarbeitung
57 der Attribute der gesicherten Datein sorgt, m\"{u}ssen Sie unbedingt darauf
58 achten, dass PostgreSQL mit der Option {\bf \verb:--:enable-thread-safety}
59 kompiliert wurde. Bei den meisten gro{\ss}en Linux-Distributionen ist das
60 der Fall. Aber falls Sie nicht sichert sind k\"{o}nnen Sie mit folgendem
61 Kommando feststellen, ob Ihr PostgreSQL gegen die pthreads-Bibliothek
66 nm /usr/lib/libpq.a | grep pthread_mutex_lock
70 Die Kommandos m\"{u}ssen in etwa eine Zeile wie diese zur\"{u}ckgeben:
78 wenn das der Fall ist, ist alles in Ordnung. Wenn keine Zeilen zur\"{u}ckgegeben
79 werden, wird Bacula beim kompilieren die "`Batch-Insert"`-Funktion deaktivieren.
80 Wenn Sie sie trotzdem benutzen wollen, m\"{u}ssen Sie PostgreSQL mit der Option
81 \verb:--:enable-thread-safety neu kompilieren.
83 Nach der PostgreSQL-Installation fahren Sie bitte mit der Installation von Bacula
84 fort. Sp\"{a}ter wenn Sie diese abgeschlossen haben, lesen Sie hier weiter, um
85 die Konfiguration von PostgreSQL zu beenden. Bitte beachten Sie, dass einige
86 Schritte im weiteren Verlauf der PostgreSQL-Konfiguration Scripte ben\"{o}tigen,
87 die erst bei der Installation von Bacula erstellt werden. Auch wenn Sie f\"{u}r
88 die Installation von PostgreSQL vorkompilierte Pakete verwendet haben (zum
89 Beispiel rpm oder deb) m\"{u}ssen Sie sp\"{a}ter hier weitermachen, um die
90 Konfiguration von PostgreSQL zu vervollst\"{a}ndigen.
92 \label{PostgreSQL_configure}
93 \section{Configuring PostgreSQL}
94 \index[general]{PostgreSQL!Configuring PostgreSQL -- }
96 At this point, you should have built and installed PostgreSQL, or already have
97 a running PostgreSQL, and you should have configured, built and installed {\bf
98 Bacula}. If not, please complete these items before proceeding.
100 Please note that the {\bf ./configure} used to build {\bf Bacula} will need to
101 include {\bf \verb:--:with-postgresql=PostgreSQL-directory}, where {\bf
102 PostgreSQL-directory} is the directory name that you specified on the
103 ./configure command for configuring PostgreSQL (if you didn't specify a
104 directory or PostgreSQL is installed in a default location, you do not need to
105 specify the directory). This is needed so that Bacula can find the necessary
106 include headers and library files for interfacing to PostgreSQL.
108 {\bf Bacula} will install scripts for manipulating the database (create,
109 delete, make tables etc) into the main installation directory. These files
110 will be of the form *\_bacula\_* (e.g. create\_bacula\_database). These files
111 are also available in the \lt{}bacula-src\gt{}/src/cats directory after
112 running ./configure. If you inspect create\_bacula\_database, you will see
113 that it calls create\_postgresql\_database. The *\_bacula\_* files are
114 provided for convenience. It doesn't matter what database you have chosen;
115 create\_bacula\_database will always create your database.
117 Now you will create the Bacula PostgreSQL database and the tables that Bacula
118 uses. These instructions assume that you already have PostgreSQL running. You
119 will need to perform these steps as a user that is able to create new
120 databases. This can be the PostgreSQL user (on most systems, this is the pgsql
124 \item cd \lt{}install-directory\gt{}
126 This directory contains the Bacula catalog interface routines.
128 \item ./create\_bacula\_database
130 This script creates the PostgreSQL {\bf bacula} database.
131 Before running this command, you should carefully think about
132 what encoding sequence you want for the text fields (paths, files, ...).
133 Ideally, the encoding should be set to UTF8. However, many Unix systems
134 have filenames that are not encoded in UTF8, either because you have
135 not set UTF8 as your default character set or because you have imported
136 files from elsewhere (e.g. MacOS X). For this reason, Bacula uses
137 SQL\_ASCII as the default encoding. If you want to change this,
138 please modify the script before running it, but be forewarned that
139 Bacula backups will fail if PostgreSQL finds any non-UTF8 sequences.
141 If running the script fails, it is probably because the database is
142 owned by a user other than yourself. On many systems, the database
143 owner is {\bf pgsql} and on others such as Red Hat and Fedora it is {\bf
144 postgres}. You can find out which it is by examining your /etc/passwd
145 file. To create a new user under either your name or with say the name
146 {\bf bacula}, you can do the following:
150 (enter root password)
151 su pgsql (or postgres)
152 createuser kern (or perhaps bacula)
153 Shall the new user be allowed to create databases? (y/n) y
154 Shall the new user be allowed to create more new users? (y/n) (choose
159 At this point, you should be able to execute the
160 ./create\_bacula\_database command.
162 \item ./make\_bacula\_tables
164 This script creates the PostgreSQL tables used by {\bf Bacula}.
165 \item ./grant\_bacula\_privileges
167 This script creates the database user {\bf bacula} with restricted access
168 rights. You may want to modify it to suit your situation. Please note that
169 this database is not password protected.
173 Each of the three scripts (create\_bacula\_database, make\_bacula\_tables, and
174 grant\_bacula\_privileges) allows the addition of a command line argument.
175 This can be useful for specifying the user name. For example, you might need
176 to add {\bf -h hostname} to the command line to specify a remote database
179 To take a closer look at the access privileges that you have setup with the
184 PostgreSQL-directory/bin/psql --command \\dp bacula
188 Also, I had an authorization problem with the password. In the end,
189 I had to modify my {\bf pg\_hba.conf} file (in /var/lib/pgsql/data on my machine)
194 local all all ident sameuser
196 local all all trust sameuser
200 This solved the problem for me, but it is not always a good thing
201 to do from a security standpoint. However, it allowed me to run
202 my regression scripts without having a password.
204 A more secure way to perform database authentication is with md5
205 password hashes. Begin by editing the {\bf pg\_hba.conf} file, and
206 just prior the the existing ``local'' and ``host'' lines, add the line:
210 local bacula bacula md5
214 and restart the Postgres database server (frequently, this can be done
215 using "`/etc/init.d/postgresql restart"' or "`service postgresql restart"') to
216 put this new authentication rule into effect.
218 Next, become the Postgres administrator, postgres, either by logging
219 on as the postgres user, or by using su to become root and then using
220 su - postgres to become postgres. Add a password to the bacula
221 database for the bacula user using:
226 bacula=# alter user bacula with password 'secret';
232 You'll have to add this password to two locations in the
233 bacula-dir.conf file: once to the Catalog resource and once to the
234 RunBeforeJob entry in the BackupCatalog Job resource. With the
235 password in place, these two lines should look something like:
239 dbname = bacula; user = bacula; password = "secret"
241 # WARNING!!! Passing the password via the command line is insecure.
242 # see comments in make_catalog_backup for details.
243 RunBeforeJob = "/etc/make_catalog_backup bacula bacula secret"
247 Naturally, you should choose your own significantly more random
248 password, and ensure that the bacula-dir.conf file containing this
249 password is readable only by the root.
251 Even with the files containing the database password properly
252 restricted, there is still a security problem with this approach: on
253 some platforms, the environment variable that is used to supply the
254 password to Postgres is available to all users of the
255 local system. To eliminate this problem, the Postgres team have
256 deprecated the use of the environment variable password-passing
257 mechanism and recommend the use of a .pgpass file instead. To use
258 this mechanism, create a file named .pgpass containing the single
263 localhost:5432:bacula:bacula:secret
267 This file should be copied into the home directory of all accounts
268 that will need to gain access to the database: typically, root,
269 bacula, and any users who will make use of any of the console
270 programs. The files must then have the owner and group set to match
271 the user (so root:root for the copy in ~root, and so on), and the mode
272 set to 600, limiting access to the owner of the file.
274 \section{Re-initializing the Catalog Database}
275 \index[general]{Database!Re-initializing the Catalog }
276 \index[general]{Re-initializing the Catalog Database }
278 After you have done some initial testing with {\bf Bacula}, you will probably
279 want to re-initialize the catalog database and throw away all the test Jobs
280 that you ran. To do so, you can do the following:
284 cd <install-directory>
287 ./grant_bacula_privileges
291 Please note that all information in the database will be lost and you will be
292 starting from scratch. If you have written on any Volumes, you must write an
293 end of file mark on the volume so that Bacula can reuse it. Do so with:
297 (stop Bacula or unmount the drive)
298 mt -f /dev/nst0 rewind
303 Where you should replace {\bf /dev/nst0} with the appropriate tape drive
304 device name for your machine.
306 \section{Installing PostgreSQL from RPMs}
307 \index[general]{PostgreSQL!Installing from RPMs}
308 \index[general]{Installing PostgreSQL from RPMs}
309 If you are installing PostgreSQL from RPMs, you will need to install
310 both the PostgreSQL binaries and the client libraries. The client
311 libraries are usually found in a devel package, so you must
323 These will be similar with most other package managers too. After
324 installing from rpms, you will still need to run the scripts that set up
325 the database and create the tables as described above.
328 \section{Converting from MySQL to PostgreSQL}
329 \index[general]{PostgreSQL!Converting from MySQL to }
330 \index[general]{Converting from MySQL to PostgreSQL }
332 The conversion procedure presented here was worked out by Norm Dressler
333 \lt{}ndressler at dinmar dot com\gt{}
335 This process was tested using the following software versions:
338 \item Linux Mandrake 10/Kernel 2.4.22-10 SMP
339 \item Mysql Ver 12.21 Distrib 4.0.15, for mandrake-linux-gnu (i586)
340 \item PostgreSQL 7.3.4
344 WARNING: Always as a precaution, take a complete backup of your databases
345 before proceeding with this process!
348 \item Shutdown bacula (cd /etc/bacula;./bacula stop)
349 \item Run the following command to dump your Mysql database:
353 mysqldump -f -t -n >bacula-backup.dmp
358 \item Make a backup of your /etc/bacula directory (but leave the original in
360 \item Go to your Bacula source directory and rebuild it to include PostgreSQL
361 support rather then Mysql support. Check the config.log file for your
362 original configure command and replace enable-mysql with enable-postgresql.
363 \item Recompile Bacula with a make and if everything compiles completely,
364 perform a make install.
365 \item Shutdown Mysql.
366 \item Start PostgreSQL on your system.
367 \item Create a bacula user in Postgres with the createuser command. Depending on
368 your Postgres install, you may have to SU to the user who has privileges to
370 \item Verify your pg\_hba.conf file contains sufficient permissions to allow
371 bacula to access the server. Mine has the following since it's on a secure
378 host all all 127.0.0.1 255.255.255.255 trust
380 NOTE: you should restart your postgres server if you
386 \item Change into the /etc/bacula directory and prepare the database and
387 tables with the following commands:
391 ./create_postgresql_database
393 ./make_postgresql_tables
395 ./grant_postgresql_privileges
400 \item Verify you have access to the database:
410 You should not get any errors.
411 \item Load your database from the Mysql database dump with:
415 psql -Ubacula bacula <bacula-backup.dmp>
420 \item Resequence your tables with the following commands:
426 SELECT SETVAL('basefiles_baseid_seq', (SELECT
427 MAX(baseid) FROM basefiles));
428 SELECT SETVAL('client_clientid_seq', (SELECT
429 MAX(clientid) FROM client));
430 SELECT SETVAL('file_fileid_seq', (SELECT MAX(fileid)
432 SELECT SETVAL('filename_filenameid_seq', (SELECT
433 MAX(filenameid) FROM filename));
435 SELECT SETVAL('fileset_filesetid_seq', (SELECT
436 MAX(filesetid) FROM fileset));
438 SELECT SETVAL('job_jobid_seq', (SELECT MAX(jobid) FROM job));
439 SELECT SETVAL('jobmedia_jobmediaid_seq', (SELECT
440 MAX(jobmediaid) FROM jobmedia));
441 SELECT SETVAL('media_mediaid_seq', (SELECT MAX(mediaid) FROM media));
442 SELECT SETVAL('path_pathid_seq', (SELECT MAX(pathid) FROM path));
444 SELECT SETVAL('pool_poolid_seq', (SELECT MAX(poolid) FROM pool));
449 \item At this point, start up Bacula, verify your volume library and perform
450 a test backup to make sure everything is working properly.
453 \section{Upgrading PostgreSQL}
454 \index[general]{Upgrading PostgreSQL }
455 \index[general]{Upgrading!PostgreSQL }
456 \index[general]{Upgrading}
457 If you upgrade PostgreSQL, you must reconfigure, rebuild, and re-install
458 Bacula otherwise you are likely to get bizarre failures. If you
459 to modify the bacula.spec file to account for the new PostgreSQL version.
460 You can do so by rebuilding from the source rpm. To do so, you may need
461 install from rpms and you upgrade PostgreSQL, you must also rebuild Bacula.
465 \index[general]{Credits }
466 Many thanks to Dan Langille for writing the PostgreSQL driver. This will
467 surely become the most popular database that Bacula supports.