There are many possible ways to install PostgreSQL on a system. For Windows, downloading the Graphical Installer and using this is the easy way. For Linux systems such as Red Hat Enterprise Linux or CentOS, we could either use
Yellow dog Updater Modified (yum) or
Red Hat Package Manager or RPM Package Manager (rpm) commands to install PostgreSQL. For Ubuntu, PostgreSQL can be installed using the
apt-get command, which in turn works with Ubuntu's Advanced Packaging Tool (APT). While these options work, we do not get to see what is happening when we execute these commands, except, of course, that the database gets installed.
Then there are situations where we might want to build from the source. Assume that all we have is one production server and one development or staging server. We are on version 9.3. Version 9.4 is about to be released and there are quite a few interesting features in 9.4 that we want to try out. If we want to install 9.4 in the test server and use it alongside 9.3, without the installations stepping on each other's toes, compiling from the source with the
--prefix= option and specifying different installation directories is the right approach. We could also set different default ports. It's also possible that the new version (source) is ready, but the package for our Linux distribution is not ready yet.
We might use a flavor of Linux for which an installation package is not available at all. Installation from source is the way forward in these situations. One advantage with installing from the source is that we don't have to worry too much about which package to download, the version of operating system (CentOS 6.3 or 6.4?), architecture (32 bit or 64 bit), and so on. These are more or less irrelevant. Of course, we should be using an operating system/architecture that is supported by the database, but that's about it! We also need to download and install all the tools and utilities necessary to compile and make the software, in this case, PostgreSQL.
So let's get down to it.
The source for PostgreSQL is available at http://www.postgresql.org/ftp/source/.
We can see a number of versions all the way down to version 1 when it was called Postgres95 and up to the latest production and beta versions. If you belong to the group who believe that one shouldn't try software that is not at least a few months old, so that its teething issues are resolved, you should opt for the last-but-one version. It's a good idea to opt for the latest stable version. The latest versions have added quite a few very useful features, such as materialized views and an improved set of JSON functions and operators.
We will use the following
wget command to download the source:
Executing this command will give us a window that looks like this:
As we can see, the tarred and gzipped source code comes to about 21 MB. As an aside, the installation files of Oracle—the big RDBMS out here—weighs over 2.2 GB.
tar -xvf postgresql-9.3.0.tar.gz
tar command is used to create or extract
TapeARchive files. In the preceding command, the
x option is used to extract,
v for verbose is used so that we can see the list of files and folders getting extracted, and the
f option is for, well, passing the name of the file, which will undergo the extraction process. We might need to provide the
z option, so the command will be
tar -xzvf if the preceding code in the
tar command does not work. Some versions of
tar are intelligent enough to figure out whether it is a gzipped file or not and will unzip it automatically. The untarred unzipped files come to around 115 MB.
Let's inspect the contents:
cd postgresql-9.3.0 find ./ -maxdepth 1 -type d
find command searches for files meeting specific criteria. Here, we instructed
find to limit itself to scanning just one level of subdirectories using
maxdepth 1. We used the
type option along with
d to tell
find that we need files of type directory, as shown in the following screenshot:
There are four directories:
src: This directory has most of the core code, namely, code for the backend processes, optimizer, storage, client utilities (such as
psql) and code to take care of replication, and so on. It also contains the makefiles for various distributions. For example, we have the files
doc: This directory has the source for documentation written in DocBook, DocBook being an application of Standard Generalized Markup Language (SGML). It's possible to generate documentation in an HTML format, PDF format, and a few other formats.
contrib: This directory is where many extensions are available. These are add-on modules that do not form part of the core installation, but can be installed as needed. For example, those who have to connect to other PostgreSQL databases can install the Foreign Data Wrapper extension:
postgres_fdw. For those who want to access the contents of a file on the server from a table, there is the
Now let's move on to the dependencies, configuration options, and the actual installation itself.