For whom are not familiar with PostgreSQL, it is an open source object-relational database system. It has more than 15 years of active development and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness. Like all other enterprise companies, we use different databases beside Oracle to lower the costs. We prefer PostgreSQL instead of Microsoft SQL Server, because it supports PL/SQL for some degree.
In this blog post, I’ll try to show how to install PostgreSQL using YUM. YUM (Yellowdog Updater, Modified) is an open-source command-line package-management utility for Linux operating systems using the RPM Package Manager. It handles library dependencies, so it’s the easiest and maybe the best way to install any RPMs. In this blog post, I’ll install PostgreSQL on Oracle Linux because I use Oracle Linux when I need a Linux OS, and its support costs are lower than Redhat Enterprise Linux. In next weeks, I’m also gonna blog about backup and replication for PostgreSQL, and maybe (not promising) blog about some key concepts of PostgreSQL such as MVCC, vacuum and vacuum freeze.
I assume that you can install Redhat or Oracle Linux. You may read my old post about installing Oracle Linux, hopefully it would be useful. Anyway, if our Linux server is ready, we can download and install Repository RPM for our server. Visit http://yum.postgresql.org.
Because 9.4 is still beta, I’ll install 9.3. So I click the link of 9.3 series, and find the link for “Oracle Enterprise Linux 6 – x86_64″ (my distro is 64bit). Instead of downloading it to my desktop, I’ll copy the link and download it on my Linux server. I connect my server using SSH as root user, and run the following commands:
wget http://yum.postgresql.org/9.3/.../pgdg-oraclelinux93-9.3-1.noarch.rpm rpm -ivh pgdg-oraclelinux93-9.3-1.noarch.rpm
I shortened the URL to make it fit on page. You should use the link you get from the website. When this package is installed, it will add YUM repository of postgresql and gpg key (so yum can validate packages). Let’s check if new YUM repository working:
yum list postgresql93* Loaded plugins: refresh-packagekit, security Available Packages postgresql93.x86_64 9.3.5-1PGDG.rhel6 pgdg93 postgresql93-contrib.x86_64 9.3.5-1PGDG.rhel6 pgdg93 postgresql93-debuginfo.x86_64 9.3.5-1PGDG.rhel6 pgdg93 postgresql93-devel.x86_64 9.3.5-1PGDG.rhel6 pgdg93 postgresql93-docs.x86_64 9.3.5-1PGDG.rhel6 pgdg93 postgresql93-jdbc.x86_64 9.3.1101-1PGDG.rhel6 pgdg93 postgresql93-jdbc-debuginfo.x86_64 9.3.1101-1PGDG.rhel6 pgdg93 postgresql93-libs.x86_64 9.3.5-1PGDG.rhel6 pgdg93 postgresql93-odbc.x86_64 09.03.0300-1PGDG.rhel6 pgdg93 postgresql93-odbc-debuginfo.x86_64 09.03.0300-1PGDG.rhel6 pgdg93 postgresql93-plperl.x86_64 9.3.5-1PGDG.rhel6 pgdg93 postgresql93-plpython.x86_64 9.3.5-1PGDG.rhel6 pgdg93 postgresql93-pltcl.x86_64 9.3.5-1PGDG.rhel6 pgdg93 postgresql93-python.x86_64 4.1.1-1PGDG.rhel6 pgdg93 postgresql93-python-debuginfo.x86_64 4.1.1-1PGDG.rhel6 pgdg93 postgresql93-server.x86_64 9.3.5-1PGDG.rhel6 pgdg93 postgresql93-test.x86_64 9.3.5-1PGDG.rhel6 pgdg93
If you see the packages from pgdg93 repository, you can use YUM to install PostgreSQL by issuing the following command (as root):
yum install postgresql93-server postgresql93-contrib
A little reminder, we’ll use root user to install packages, create directories etc, and then we’ll switch to postgres user (it’s created by postgressql package) and complete the initialization of database server. I also install postgresql93-contrib because it provides some useful utilities such as pgbench, pg_upgrade, pg_archivecleanup and some extensions. After you install PostgreSQL, let’s create a directory for our databases and change ownership of the folder to postgres user, and change the password of postgres user:
mkdir /postgres chown postgres:postgres /postgres passwd postgres
Now we can switch to postgres user to initialize and start our PostgreSQL service. Initialization needs to be done only for one time on the server. It will create the configuration, information schema (metadata) and folders for PostgreSQL.
su - postgres /usr/pgsql-9.3/bin/initdb -D /postgres The files belonging to this database system will be owned by user "postgres". This user must also own the server process. The database cluster will be initialized with locale "en_US.UTF-8". The default database encoding has accordingly been set to "UTF8". The default text search configuration will be set to "english". Data page checksums are disabled. fixing permissions on existing directory /postgres ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers ... 128MB creating configuration files ... ok creating template1 database in /postgres/base/1 ... ok initializing pg_authid ... ok initializing dependencies ... ok creating system views ... ok loading system objects' descriptions ... ok creating collations ... ok creating conversions ... ok creating dictionaries ... ok setting privileges on built-in objects ... ok creating information schema ... ok loading PL/pgSQL server-side language ... ok vacuuming database template1 ... ok copying template1 to template0 ... ok copying template1 to postgres ... ok syncing data to disk ... ok WARNING: enabling "trust" authentication for local connections You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb. Success. You can now start the database server using: /usr/pgsql-9.3/bin/postgres -D /postgres or /usr/pgsql-9.3/bin/pg_ctl -D /postgres -l logfile start
Now PostgreSQL server is ready to start. Before we start it, let’s modify .bash_profile file (in postgres home folder) and make it similar to the below:
cat ~/.bash_profile [ -f /etc/profile ] && source /etc/profile export PGDATA=/postgres export PATH=$PATH:/usr/pgsql-9.3/bin/ export PS1='\u@\h \W]\$ '
Now let’s start the server. If you declared the PATH and PGDATA variables, you can just start it with “pg_ctl start”. PG_CTL is the tool you need to use to stop, start, reload (to read configuration changes) PostgreSQL server. If you didn’t declare the variables yet, run the following command to start the server:
/usr/pgsql-9.3/bin/pg_ctl -D /postgres
It will show you a warning about logging and it will look like it will not get back to command line. But in fact it just doesn’t show the command line prompt. Press enter again to see the prompt. Let’s check the processes:
ps -ef | grep postgres root 1325 2797 0 12:24 pts/0 00:00:00 su - postgres postgres 1327 1325 0 12:24 pts/0 00:00:00 -bash postgres 1435 1 0 12:27 pts/0 00:00:00 /usr/pgsql-9.3/bin/postgres postgres 1437 1435 0 12:27 ? 00:00:00 postgres: logger process postgres 1439 1435 0 12:27 ? 00:00:00 postgres: checkpointer process postgres 1440 1435 0 12:27 ? 00:00:00 postgres: writer process postgres 1441 1435 0 12:27 ? 00:00:00 postgres: wal writer process postgres 1442 1435 0 12:27 ? 00:00:00 postgres: autovacuum launcher postgres 1443 1435 0 12:27 ? 00:00:00 postgres: stats collector process postgres 1764 1327 0 13:17 pts/0 00:00:00 ps -ef postgres 1765 1327 0 13:17 pts/0 00:00:00 grep postgres
Everything seems OK. Let’s create a test database and login it via PSQL. PSQL is a PostgreSQL client similar to Oracle SQL/Plus and it provides some useful meta-commands in addition to run SQL commands. The -d parameter says which database we want to connect. We can create databases using psql or command line tool “createdb”:
createdb test # yes we're still in bash psql -d test
Now we have a working PostgreSQL server to explore. When you connect to PostgreSQL using PSQL, you can get help for SQL commands by typing \h (or \help). You can also get help for meta-commands by typing \?. Most people prefer GUI for running queries, checking tables etc. Although there are several GUI based clients for PostgreSQL, I recommend you to use pgAdmin. It runs on Linux, Mac OS X and Windows. If you’ll connect this server from a remote client, you need to make sure that PostgreSQL listens the network (not only localhost), so go to our database directory, edit postgresql.conf file and restart the server:
cd $PGDATA echo "listen_addresses='*'" >> postgresql.conf pg_ctl restart
You also need to give permissions to the IP address of your computer, so you need to edit pg_hba.conf:
cd $PGDATA echo 'host all all 10.0.0.1/32 trust' >> pg_hba.conf pg_ctl reload
This will let us connect to our server from 10.0.0.1 without password. You can read more about pg_hba.conf and client authentication in PostgreSQL documents.
I hope this helps.