In this section, you take a closer look at the installation process of PostgreSQL on various platforms. Installing on UNIX SystemsIn contrast to Windows systems, UNIX systems can have many different faces; therefore the installation process on various UNIX systems can differ significantly. Many Linux systems, such as RedHat and Debian Linux, provide package management software for simplifying the installation of PostgreSQL binaries or source code. This section provides an overview of how PostgreSQL is installed on UNIX systems. Installing on RPM-Based SystemsPackage management has always been a very desirable feature on Linux systems. With the help of the RedHat Package Manager (RPM), Linux offers a simple and efficient way to manage software packages on Linux systems. The RPM has been developed by RedHat and is available under the terms of the GPL. Packaging software means that files belonging together are compiled into an archive. But archiving files is not the key feature of a package manager; software products can be installed and uninstalled easily with the help of a simple shell command or GUIs available on the Internet. It is also possible to assign rules to a package. Imagine a software product A that needs a library called B to be compiled correctly. If somebody wants to install the RPM package of A on a system, it seems logical that it can be installed only when package B is also available. RPM takes care of details such as that and makes sure that no packages can be installed or uninstalled that are still needed by other software components . RPM keeps information about all packages installed in a small database, so it is an easy task to find out whether a package is installed on your system. If you want to find out whether PostgreSQL is installed on your machine, try the following command: [root@notebook hs]$ rpm -qva grep postgres postgresql-libs-7.1.2-4PGDG postgresql-python-7.1.2-4PGDG postgresql-7.1.2-4PGDG postgresql-test-7.1.2-4PGDG postgresql-perl-7.1.2-4PGDG postgresql-docs-7.1.2-4PGDG postgresql-tcl-7.1.2-4PGDG postgresql-odbc-7.1.2-4PGDG postgresql-devel-7.1.2-4PGDG postgresql-server-7.1.2-4PGDG postgresql-contrib-7.1.2-4PGDG postgresql-tk-7.1.2-4PGDG postgresql-jdbc-7.1.2-4PGDG The rpm -qva command lists all packages found on the system. You find out all packages related to PostgreSQL by using a simple grep command. On most Linux distributions, such as RedHat or Suse, PostgreSQL is already included and you do not have to install it manually. Tip It is possible that you won't find all packages, because some ”such as the PHP's PostgreSQL package ”do not contain the string postgres . Try searching for something like pgsql as well. If no packages are found on the system, PostgreSQL is not installed. If you don't have the packages on one of your Linux CDs or if you want to upgrade your PostgreSQL installation, you have to download the sources from a mirror near you. To find the best mirror for downloading the files, check out www.postgresql.org. After finding the best mirror site for your location, click Software. Choose an FTP server near you and go to the binary directory of the FTP server. There you will find several subdirectories, where you can find the appropriate binaries for your system. Usually, binaries for x86 CPUs and the most widespread Linux distributions are available. If you do not have an x86 compatible processor (Intel Pentium or AMD, for example), you might have to compile the sources manually. To download the software, use a simple FTP client, a Web browser such as Netscape, Konqueror, Opera, or Internet Explorer. wget is another tool for downloading software and is included in most recent Linux distributions. wget is a powerful command-line tool for downloading and enables the user to start downloads as background processes (in combination with & ). Now that you have downloaded all RPM packages on your machine, you can start installing the software. To install the database you use a simple command (assume that your current directory contains only PostgreSQL binaries): [root@notebook hs]$ rpm -Uvh *rpm postgresql-tk ################################################## postgresql-test ################################################## postgresql-tcl ################################################## postgresql-server ################################################## postgresql-python ################################################## postgresql-perl ################################################## postgresql-odbc ################################################## postgresql-libs ################################################## postgresql-jdbc ################################################## postgresql-docs ################################################## postgresql-devel ################################################## postgresql-contrib ################################################## postgresql ################################################## If no packages are missing, the installation is ready ”everything is done by RPM automatically. Updating packages that are already installed on the system works the same way as installing packages. Uninstalling the software is as simple as installing the packages. Use this command: [root@notebook hs]$ rpm -e postgresql postgresql-jdbc postgresql-tk postgresql- contrib postgresql-server postgresql-tcl postgresql-devel postgresql-odbc postgresql-perl postgresql-test postgresql-docs postgresql-python postgresql- libs If no errors are displayed, the packages have been uninstalled correctly. Sometimes uninstalling does not work because PostgreSQL is needed by other packages. In that case, you either have to uninstall those packages related to PostgreSQL or leave everything on the server. Installing on Debian-Based SystemsDebian is a Linux distribution that is not based on the RPM. Debian's package management tool is called dselect and can be compared with RPM. Both tools support a simple interface for installing packages from the command line. To install a PostgreSQL package for Debian on your Debian system, use the dpkg : [root@debian deb]$ dpkg -i *deb This command installs all Debian packages in the current directory and is equal to the following: [root@debian deb]$ dpkg -install *deb If you cannot find recent Debian packages of PostgreSQL, you can easily convert RPM files to DEB files by using a program called Alien. To find out more about Alien, check out http://kitenet.net/programs/alien/. Installing the Source CodeSome people prefer to install the source code themselves instead of relying on precompiled software packages. This has significant advantages because you know what is inside the binaries and what you have done. Compiling PostgreSQL manually is also necessary when you want to install the most recent version of PostgreSQL, because there might be no binary packages available for your system and CPU yet. Before you get to the installation process, you have to download the sources from a PostgreSQL mirror near you. The easiest way to download the source code is to use wget : [hs@notebook hs]$ wget ftp://ftp.postgresql.org/pub/postgresql-x.x.x.tar.gz To unpack the source, you use gzip and tar : [hs@notebook hs]$ tar xvfz postgresql-7.1.2.tar.gz If the z flag (which automatically unzips the archive) is not supported by your version of tar , the package can also be extracted like this: [hs@notebook hs]$ gunzip -c postgresql-7.1.2.tar.gz tar xv This command creates a subdirectory containing the PostgreSQL package, so you need to install the database. The first thing to do when installing PostgreSQL is to run configure . The configure script has a lot of options that you can use to configure PostgreSQL exactly to your needs. Here is an overview of PostgreSQL's configure script: [hs@notebook postgresql-7.1.2]$ ./configure --help Usage: configure [options] [host] Options: [defaults in brackets after descriptions] Configuration: --cache-file=FILE cache test results in FILE --help print this message --no-create do not create output files --quiet, --silent do not print `checking...' messages --version print the version of autoconf that created configure Directory and file names: --prefix=PREFIX install architecture-independent files in PREFIX [/usr/local/pgsql] --exec-prefix=EPREFIX install architecture-dependent files in EPREFIX [same as prefix] --bindir=DIR user executables in DIR [EPREFIX/bin] --sbindir=DIR system admin executables in DIR [EPREFIX/sbin] --libexecdir=DIR program executables in DIR [EPREFIX/libexec] --datadir=DIR read-only architecture-independent data in DIR [PREFIX/share] --sysconfdir=DIR read-only single-machine data in DIR [PREFIX/etc] --sharedstatedir=DIR modifiable architecture-independent data in DIR [PREFIX/com] --localstatedir=DIR modifiable single-machine data in DIR [PREFIX/var] --libdir=DIR object code libraries in DIR [EPREFIX/lib] --includedir=DIR C header files in DIR [PREFIX/include] --oldincludedir=DIR C header files for non-gcc in DIR [/usr/include] --docdir=DIR doc documentation in DIR [PREFIX/doc] --mandir=DIR man documentation in DIR [PREFIX/man] --srcdir=DIR find the sources in DIR [configure dir or ..] --program-prefix=PREFIX prepend PREFIX to installed program names --program-suffix=SUFFIX append SUFFIX to installed program names --program-transform-name=PROGRAM run sed PROGRAM on installed program names Host type: --build=BUILD configure for building on BUILD [BUILD=HOST] --host=HOST configure for HOST [guessed] --target=TARGET configure for TARGET [TARGET=HOST] Features and packages: --disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no) --enable-FEATURE[=ARG] include FEATURE [ARG=yes] --with-PACKAGE[=ARG] use PACKAGE [ARG=yes] --without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no) --x-includes=DIR X include files are in DIR --x-libraries=DIR X library files are in DIR --enable and --with options recognized: --with-includes=DIRS look for additional header files in DIRS --with-libraries=DIRS look for additional libraries in DIRS --with-libs=DIRS alternative spelling of --with-libraries --enable-locale enable locale support --enable-recode enable character set recode support --enable-multibyte enable multibyte character support --enable-unicode-conversion enable unicode conversion support --with-pgport=PORTNUM change default port number [5432] --with-maxbackends=N set default maximum number of connections [32] --disable-shared do not build shared libraries --disable-rpath do not embed shared library search path in executables --enable-debug build with debugging symbols (-g) --enable-depend turn on automatic dependency tracking --enable-cassert enable assertion checks (for debugging) --with-tcl build Tcl and Tk interfaces --without-tk do not build Tk interfaces if Tcl is enabled --with-tclconfig=DIR tclConfig.sh and tkConfig.sh are in DIR --with-tkconfig=DIR tkConfig.sh is in DIR --with-perl build Perl interface and PL/Perl --with-python build Python interface module --with-java build JDBC interface and Java tools --with-krb4[=DIR] build with Kerberos 4 support [/usr/athena] --with-krb5[=DIR] build with Kerberos 5 support [/usr/athena] --with-krb-srvnam=NAME name of the service principal in Kerberos [postgres] --with-openssl[=DIR] build with OpenSSL support [/usr/local/ssl] --enable-odbc build the ODBC driver package --with-odbcinst=DIR default directory for odbcinst.ini [sysconfdir] --with-CXX build C++ modules (libpq++) --with-gnu-ld assume the C compiler uses GNU ld [default=no] --enable-syslog enable logging to syslog As you can see, the number of options you can define is nearly endless. Before you start compiling the software, let's take a closer look at some of these options:
To continue the installation process, you create a group and a user called postgres . The user does not have to be called postgres , but people normally use that name for running the database server. To add the group and the user, you use these two commands: [root@notebook postgresql-7.1.2]$ groupadd postgres [root@notebook postgresql-7.1.2]$ adduser postgres -g postgres You want the PostgreSQL binaries to be installed in /usr/local/postgresql; the database should be located in /data/postgresql. Both directories can be created by using a simple mkdir command. The two directories should be owned by the postgres user, so you have to change the permissions of the directories with the help of the chown (change owner) command. Now that precompilation is finished, you can write a small shell script. Using a script has many advantages: On the one hand it documents how you have compiled the server, and on the other hand you can easily use the same script for updating PostgreSQL. Documentation is important and can save you a lot of headaches . Here is a simple script called compile.sh: #!/bin/sh # configuring the PostgreSQL server env CFLAGS='-O3' ./configure --cache-file=/tmp/postgres_test.txt \ --prefix=/usr/local/postgresql --enable-multibyte\ --datadir=/data/postgresql --with-maxbackends=128 \ --with-tcl --with-perl --with-python --enable-odbc \ --with-CXX # building the binaries make # installing the binaries make install Let's have a closer look. First, you call configure with the parameters you want PostgreSQL to be compiled with (Perl, Python, multibyte support, and so forth). You also pass an additional compiler flag to the script by using env . The -O3 option does everything -O2 does, and also turns on -finline functions . Passing compiler options to configure is not necessary; don't use it if you don't know exactly what you are doing. To show you an easier way to compile the server, here is another script: #/bin/sh ./configure --prefix=/usr/local/postgresql --datadir=/data/postgresql --with-tcl \ --with-perl --with-python --enable-odbc --with-CXX make make install Let's get back to the more complex installation script ”we will use it for the rest of this section. To execute the script you add execute rights to compile.sh by typing the following: [root@notebook postgresql-7.1.2]$ chmod +x compile.sh Now that the shell script is ready and the correct rights are set, you can start the installation process by executing your script: [root@notebook postgresql-7.1.2]$ ./compile.sh Configuring and compiling the server takes some time, but when the script is ready, you can go on with the installation process. In the next step, you initialize the database templates that you will need to work with PostgreSQL. Therefore, you have to use the initdb command, which can be found in the bin directory of the database server. Here is an overview of the command's syntax: [postgres@notebook bin]$ ./initdb --help initdb initializes a PostgreSQL database cluster. Usage: initdb [options] datadir Options: [-D, --pgdata] DATADIR Location for this database cluster -W, --pwprompt Prompt for a password for the new superuser -E, --encoding ENCODING Set the default multibyte encoding for new databases -i, --sysid SYSID Database sysid for the superuser Less commonly used options: -L DIRECTORY Where to find the input files -d, --debug Generate lots of debugging output -n, --noclean Do not clean up after errors Report bugs to <pgsql-bugs@postgresql.org>. Now you can initialize the database: [postgres@notebook bin]$ ./initdb -L /data/postgresql -D /data/postgresql/data -L defines the directory containing the input (template) files for your database (defined by --datadir in compile.sh). Note The extension of the template files is .bki. Some more recent PostgreSQL distributions store the template files in a subfolder of datadir; the extension of the templates will help you find the right directory. -D defines the location where your databases are going to reside, the place where all the data you will insert will be stored by PostgreSQL. Initializing a new database should, in most cases, work without any problems. The database has successfully been installed now and you can start the PostgreSQL daemon. Installing on WindowsWindows differs from UNIX-based operating systems, but PostgreSQL is a flexible piece of software. Like most open source programmers, the PostgreSQL developers have implemented a software that is capable of running on various platforms ”one of those platforms is Microsoft Windows. The easiest way to install PostgreSQL on Windows is to use Cygwin, a UNIX-like environment that provides all tools commonly known under UNIX (for example, Bash, gcc, and so forth). With the help of the Cygnus tools, it is easy to install PostgreSQL under Windows. To install Cygwin on your system, check out http://sources.redhat.com/cygwin/ and press the Install Cygwin now button to download a file called setup.exe. This file contains the installation process for the software. Start setup.exe and you will be guided through the installation process. A binary version of PostgreSQL is included in the Cygwin distribution, so you do not have to worry about installing PostgreSQL separately. After installing Cygwin, you have to initialize a PostgreSQL database. Therefore, you have to install the cygipc package available at: http://www.neuro.gatech.edu/users/cwilson/cygutils/v1.1/index.html) This can be done by extracting the tar archive containing the software in the root directory of the Cygnus environment. Installing this package is necessary to run the server because PostgreSQL needs System V IPC system calls. ipc is a common kernel entry point for the System V IPC calls for messages, semaphores, and shared memory. If the package is not installed, PostgreSQL can't initialize the database. Now that the software is installed, you have to add the bin directory in the Cygnus environment to the environment variable PATH of your Windows system; otherwise , cygipc won't be able to find the DLLs it needs for starting. First you install the daemon as a service: ipc-daemon --install-as-service Then you start the daemon: net start ipc-daemon # NT/2000 or ipc-daemon & # 9X/Me After that you can go on with PostgreSQL by initializing the database with the initdb command: initdb -L /usr/share/postgresql -D /data/postgresql -L defines the location of the templates PostgreSQL must use it to generate the system databases. -D defines the directory in which to store the files of the database you want to generate using initdb . Keep in mind that the directory specified by -D must be empty or nonexistent. |