]> git.sur5r.net Git - bacula/docs/blob - docs/manual/postgresql.tex
kes Make sure valid argument passed to str_to_utime() where Arno
[bacula/docs] / docs / manual / postgresql.tex
1 %%
2 %%
3
4 \section*{Installing and Configuring PostgreSQL}
5 \label{_ChapterStart10}
6 \index[general]{PostgreSQL!Installing and Configuring }
7 \index[general]{Installing and Configuring PostgreSQL }
8 \addcontentsline{toc}{section}{Installing and Configuring PostgreSQL}
9
10 Warning!!! 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 \subsection*{Installing and Configuring PostgreSQL -- Phase I}
22 \index[general]{Installing and Configuring PostgreSQL -- Phase I }
23 \index[general]{Phase I!Installing and Configuring PostgreSQL -- }
24 \addcontentsline{toc}{subsection}{Installing and Configuring PostgreSQL --
25 Phase I}
26
27 If you use the {\bf ./configure \verb:--:with-postgresql=PostgreSQL-Directory}
28 statement for configuring {\bf Bacula}, you will need PostgreSQL version 7.3
29 or later installed. NOTE! PostgreSQL versions earlier than 7.3 do not work
30 with Bacula. If PostgreSQL is installed in the standard system location, you
31 need only enter {\bf \verb:--:with-postgresql} since the configure program will
32 search all the standard locations. If you install PostgreSQL in your home
33 directory or some other non-standard directory, you will need to provide the
34 full path with the {\bf \verb:--:with-postgresql} option. 
35
36 Installing and configuring PostgreSQL is not difficult but can be confusing
37 the first time. If you prefer, you may want to use a package provided by your
38 chosen operating system. Binary packages are available on most PostgreSQL
39 mirrors. 
40
41 If you prefer to install from source, we recommend following the instructions
42 found in the 
43 \elink{PostgreSQL documentation}{http://www.postgresql.org/docs/}. 
44
45 If you are using FreeBSD, 
46 \elink{this FreeBSD Diary article}{http://www.freebsddiary.org/postgresql.php}
47 will be useful. Even if you are not using FreeBSD, the article will contain
48 useful configuration and setup information. 
49
50 After installing PostgreSQL, you should return to completing the installation
51 of {\bf Bacula}. Later, after Bacula is installed, come back to this chapter
52 to complete the installation. Please note, the installation files used in the
53 second phase of the PostgreSQL installation are created during the Bacula
54 Installation. 
55 \label{PostgreSQL_phase2}
56
57 \subsection*{Installing and Configuring PostgreSQL -- Phase II}
58 \index[general]{Phase II!Installing and Configuring PostgreSQL -- }
59 \index[general]{Installing and Configuring PostgreSQL -- Phase II }
60 \addcontentsline{toc}{subsection}{Installing and Configuring PostgreSQL --
61 Phase II}
62
63 At this point, you should have built and installed PostgreSQL, or already have
64 a running PostgreSQL, and you should have configured, built and installed {\bf
65 Bacula}. If not, please complete these items before proceeding. 
66
67 Please note that the {\bf ./configure} used to build {\bf Bacula} will need to
68 include {\bf \verb:--:with-postgresql=PostgreSQL-directory}, where {\bf
69 PostgreSQL-directory} is the directory name that you specified on the
70 ./configure command for configuring PostgreSQL (if you didn't specify a
71 directory or PostgreSQL is installed in a default location, you do not need to
72 specify the directory). This is needed so that Bacula can find the necessary
73 include headers and library files for interfacing to PostgreSQL. 
74
75 {\bf Bacula} will install scripts for manipulating the database (create,
76 delete, make tables etc) into the main installation directory. These files
77 will be of the form *\_bacula\_* (e.g. create\_bacula\_database). These files
78 are also available in the \lt{}bacula-src\gt{}/src/cats directory after
79 running ./configure. If you inspect create\_bacula\_database, you will see
80 that it calls create\_postgresql\_database. The *\_bacula\_* files are
81 provided for convenience. It doesn't matter what database you have chosen;
82 create\_bacula\_database will always create your database. 
83
84 Now you will create the Bacula PostgreSQL database and the tables that Bacula
85 uses. These instructions assume that you already have PostgreSQL running. You
86 will need to perform these steps as a user that is able to create new
87 databases. This can be the PostgreSQL user (on most systems, this is the pgsql
88 user). 
89
90 \begin{enumerate}
91 \item cd \lt{}install-directory\gt{}
92
93    This directory contains the Bacula catalog  interface routines.  
94
95 \item ./create\_bacula\_database
96
97    This script creates the PostgreSQL {\bf bacula} database.  
98    If it fails, it is probably because the database is owned by a
99    user other than yourself. On many systems, the database owner is
100    {\bf pgsql} and on others such as RedHat and Fedora it is {\bf postgres}.
101    You can find out which it is by examining your /etc/passwd file.
102    To create a new user under either your name or with say the name
103    {\bf bacula}, you can do the following:
104
105 \begin{verbatim}
106    su
107    (enter root password)
108    su pgsql (or postgres)
109    createuser kern (or perhaps bacula)
110    Shall the new user be allowed to create databases? (y/n) y
111    Shall the new user be allowed to create more new users? (y/n) (choose
112          what you want)
113    exit
114 \end{verbatim}
115     
116     At this point, you should be able to execute the
117     ./create\_bacula\_database command.
118
119 \item ./make\_bacula\_tables
120
121    This script creates the PostgreSQL tables used by {\bf Bacula}.  
122 \item ./grant\_bacula\_privileges
123
124    This script creates the database user {\bf bacula}  with restricted access
125 rights. You may  want to modify it to suit your situation. Please note that 
126 this database is not password protected.  
127
128 \end{enumerate}
129
130 Each of the three scripts (create\_bacula\_database, make\_bacula\_tables, and
131 grant\_bacula\_privileges) allows the addition of a command line argument.
132 This can be useful for specifying the user name. For example, you might need
133 to add {\bf -h hostname} to the command line to specify a remote database
134 server. 
135
136 To take a closer look at the access privileges that you have setup with the
137 above, you can do: 
138
139 \footnotesize
140 \begin{verbatim}
141 PostgreSQL-directory/bin/psql --command \\dp bacula
142 \end{verbatim}
143 \normalsize
144
145 Also, I had an authorization problem with the password. In the end,
146 I had to modify my {\bf pg\_hba.conf} file (in /var/lib/pgsql/data on my machine)
147 from:
148
149 \footnotesize
150 \begin{verbatim}
151   local   all    all        ident  sameuser
152 to
153   local   all    all        trust  sameuser
154 \end{verbatim}
155 \normalsize
156
157 This solved the problem for me, but it is not always a good thing
158 to do from a security standpoint.  However, it allowed me to run
159 my regression scripts without having a password.
160
161 A more secure way to perform database authentication is with md5
162 password hashes.  Begin by editing the {\bf pg\_hba.conf} file, and
163 just prior the the existing ``local'' and ``host'' lines, add the line:
164
165 \footnotesize
166 \begin{verbatim}
167   local bacula bacula md5
168 \end{verbatim}
169 \normalsize
170
171 and restart the Postgres database server (frequently, this can be done
172 using "/etc/init.d/postgresql restart") to put this new authentication
173 rule into effect.
174
175 Next, become the Postgres administrator, postgres, either by logging
176 on as the postgres user, or by using su to become root and then using
177 su - postgres to become postgres.  Add a password to the bacula
178 database for the bacula user using:
179
180 \footnotesize
181 \begin{verbatim}
182   \$ psql bacula
183   bacula=# alter user bacula with password 'secret';
184   ALTER USER
185   bacula=# \\q
186 \end{verbatim}
187 \normalsize
188
189 Next, you'll have to add this password to two locations in the
190 bacula-dir.conf file: once to the Catalog resource and once to the
191 RunBeforeJob entry in the BackupCatalog Job resource.  With the
192 password in place, these two lines should look something like:
193
194 \footnotesize
195 \begin{verbatim}
196   dbname = bacula; user = bacula; password = "secret"
197     ... and ...
198   RunBeforeJob = "/etc/make_catalog_backup bacula bacula secret"
199 \end{verbatim}
200 \normalsize
201
202 Naturally, you should choose your own significantly more random
203 password, and ensure that the bacula-dir.conf file containing this
204 password is readable only by the root.
205
206 Even with the files containing the database password properly
207 restricted, there is still a security problem with this approach: on
208 some platforms, the environment variable that is used to supply the
209 password to Postgres is available to all users of the
210 local system.  To eliminate this problem, the Postgres team have
211 deprecated the use of the environment variable password-passing
212 mechanism and recommend the use of a .pgpass file instead.  To use
213 this mechanism, create a file named .pgpass containing the single
214 line:
215
216 \footnotesize
217 \begin{verbatim}
218   localhost:5432:bacula:bacula:secret
219 \end{verbatim}
220 \normalsize
221
222 This file should be copied into the home directory of all accounts
223 that will need to gain access to the database: typically, root,
224 bacula, and any users who will make use of any of the console
225 programs.  The files must then have the owner and group set to match
226 the user (so root:root for the copy in ~root, and so on), and the mode
227 set to 600, limiting access to the owner of the file.
228
229 \subsection*{Re-initializing the Catalog Database}
230 \index[general]{Database!Re-initializing the Catalog }
231 \index[general]{Re-initializing the Catalog Database }
232 \addcontentsline{toc}{subsection}{Re-initializing the Catalog Database}
233
234 After you have done some initial testing with {\bf Bacula}, you will probably
235 want to re-initialize the catalog database and throw away all the test Jobs
236 that you ran. To do so, you can do the following: 
237
238 \footnotesize
239 \begin{verbatim}
240   cd <install-directory>
241   ./drop_bacula_tables
242   ./make_bacula_tables
243   ./grant_bacula_privileges
244 \end{verbatim}
245 \normalsize
246
247 Please note that all information in the database will be lost and you will be
248 starting from scratch. If you have written on any Volumes, you must write an
249 end of file mark on the volume so that Bacula can reuse it. Do so with: 
250
251 \footnotesize
252 \begin{verbatim}
253    (stop Bacula or unmount the drive)
254    mt -f /dev/nst0 rewind
255    mt -f /dev/nst0 weof
256 \end{verbatim}
257 \normalsize
258
259 Where you should replace {\bf /dev/nst0} with the appropriate tape drive
260 device name for your machine. 
261
262 \subsection*{Installing PostgreSQL from RPMs}
263 \index[general]{PostgreSQL!Installing from RPMs}
264 \index[general]{Installing PostgreSQL from RPMs}
265 \addcontentsline{toc}{subsection}{Installing PostgreSQL from RPMs}
266 If you are installing PostgreSQL from RPMs, you will need to install
267 both the PostgreSQL binaries and the client libraries.  The client
268 libraries are usually found in a devel package, so you must
269 install:
270
271 \footnotesize
272 \begin{verbatim}
273   postgresql
274   postgresql-devel
275 \end{verbatim}
276 \normalsize
277
278 This will be the same with most other package managers too.
279
280
281 \subsection*{Converting from MySQL to PostgreSQL}
282 \index[general]{PostgreSQL!Converting from MySQL to }
283 \index[general]{Converting from MySQL to PostgreSQL }
284 \addcontentsline{toc}{subsection}{Converting from MySQL to PostgreSQL}
285
286 The conversion procedure presented here was worked out by Norm Dressler
287 \lt{}ndressler at dinmar dot com\gt{} 
288
289 This process was tested using the following software versions: 
290
291 \begin{itemize}
292 \item Linux Mandrake 10/Kernel 2.4.22-10 SMP 
293 \item Mysql Ver 12.21 Distrib 4.0.15, for mandrake-linux-gnu (i586) 
294 \item PostgreSQL 7.3.4 
295 \item Bacula 1.34.5 
296    \end{itemize}
297
298 WARNING: Always as a precaution, take a complete backup of your databases
299 before proceeding with this process! 
300
301 \begin{enumerate}
302 \item Shutdown bacula (cd /etc/bacula;./bacula stop)  
303 \item Run the following command to dump your Mysql database:  
304
305    \footnotesize
306 \begin{verbatim}
307        mysqldump -f -t -n >bacula-backup.dmp
308     
309 \end{verbatim}
310 \normalsize
311
312 \item Make a backup of your /etc/bacula directory (but leave the  original in
313    place).  
314 \item Go to your Bacula source directory and rebuild it to include  PostgreSQL
315    support rather then Mysql support. Check the  config.log file for your
316    original configure command and replace  enable-mysql with enable-postgresql.  
317 \item Recompile Bacula with a make and if everything compiles  completely,
318    perform a make install.  
319 \item Shutdown Mysql. 
320 \item Start PostgreSQL on your system.  
321 \item Create a bacula user in Postgres with the createuser command.  Depending on
322    your Postgres install, you may have to SU to the  user who has privileges to
323    create a user.  
324 \item Verify your pg\_hba.conf file contains sufficient permissions to  allow
325    bacula to access the server. Mine has the following since  it's on a secure
326    network:  
327
328 \footnotesize
329 \begin{verbatim}
330 local all all trust
331                 
332 host all all 127.0.0.1 255.255.255.255 trust
333                 
334 NOTE: you should restart your postgres server if you
335       made changes
336       
337 \end{verbatim}
338 \normalsize
339
340 \item Change into the /etc/bacula directory and prepare the database  and
341    tables with the following commands:  
342
343 \footnotesize
344 \begin{verbatim}
345 ./create_postgresql_database
346                                 
347 ./make_postgresql_tables
348                                 
349 ./grant_postgresql_privileges
350        
351 \end{verbatim}
352 \normalsize
353
354 \item Verify you have access to the database:  
355
356    \footnotesize
357 \begin{verbatim}
358   
359 psql -Ubacula bacula
360       
361 \end{verbatim}
362 \normalsize
363
364 You should not get any errors.  
365 \item Load your database from the Mysql database dump with:  
366
367    \footnotesize
368 \begin{verbatim}
369 psql -Ubacula bacula <bacula-backup.dmp>
370       
371 \end{verbatim}
372 \normalsize
373
374 \item Resequence your tables with the following commands:  
375
376    \footnotesize
377 \begin{verbatim}
378 psql -Ubacula bacula
379                 
380 SELECT SETVAL('basefiles_baseid_seq', (SELECT
381 MAX(baseid) FROM basefiles));
382 SELECT SETVAL('client_clientid_seq', (SELECT
383 MAX(clientid) FROM client));
384 SELECT SETVAL('file_fileid_seq', (SELECT MAX(fileid)
385 FROM file));
386 SELECT SETVAL('filename_filenameid_seq', (SELECT
387 MAX(filenameid) FROM filename));
388                 
389 SELECT SETVAL('fileset_filesetid_seq', (SELECT
390 MAX(filesetid) FROM fileset));
391                 
392 SELECT SETVAL('job_jobid_seq', (SELECT MAX(jobid) FROM job));
393 SELECT SETVAL('jobmedia_jobmediaid_seq', (SELECT
394 MAX(jobmediaid) FROM jobmedia));
395 SELECT SETVAL('media_mediaid_seq', (SELECT MAX(mediaid) FROM media));
396 SELECT SETVAL('path_pathid_seq', (SELECT MAX(pathid) FROM path));
397                 
398 SELECT SETVAL('pool_poolid_seq', (SELECT MAX(poolid) FROM pool));
399        
400 \end{verbatim}
401 \normalsize
402
403 \item At this point, start up Bacula, verify your volume library and  perform
404    a test backup to make sure everything is working  properly. 
405 \end{enumerate}
406
407 \subsection*{Upgrading PostgreSQL}
408 \index[general]{Upgrading PostgreSQL }
409 \index[general]{Upgrading!PostgreSQL }
410 \addcontentsline{toc}{subsection}{Upgrading PostgreSQL}
411 If you upgrade PostgreSQL, you must reconfigure, rebuild, and re-install 
412 Bacula otherwise you are likely to get bizarre failures.  If you
413 to modify the bacula.spec file to account for the new PostgreSQL version.
414 You can do so by rebuilding from the source rpm. To do so, you may need
415 install from rpms and you upgrade PostgreSQL, you must also rebuild Bacula.
416
417
418 \subsection*{Credits}
419 \index[general]{Credits }
420 \addcontentsline{toc}{subsection}{Credits}
421 Many thanks to Dan Langille for writing the PostgreSQL driver. This will
422 surely become the most popular database that Bacula supports.