Skip to main content

The Pi as a PostgreSQL Database Server

Raspbian with PostgreSQL it is quite easy actually. Just like on Ubuntu/Linux Mint/... (Replace the ellipsis with any derivative of Debian or Ubuntu.) The hardest part is to decide which version of the database server to employ. On this page the full set of options for retrieving the server is given with the necessary amount of detail.

    "Should I get it?"
Actually, since PostgreSQL (together with MySQL) is one of the most popular open source databases within the Linux realm, some distributions choose to deliver it pre-installed on their releases. If you are not sure, if you need to get the server at all, this simple command can answer that question:
$ ps aux | grep postgers
It will search through the processes running on your system and filter them to leave only those bound to PostgreSQL.

It is possible that the server is present on the system, but it is not running at the moment. In that case it is enough to see if its configurations are in place. The place in question for the 9.1 version of the server is:
/etc/postgresql/9.1/main/
Listing this directory should give out the existence of several important files. Actually the sole existence of the /etc/postgersql folder (and everything below it) is a hint that the PostgerSQL database have been installed on the system.

    Different installation processes
When you have to do everything by yourself, the simplest choice is to get the default version form the repository. Currently this is the 9.1 version. The command is trivial, and here you can see an example of the full installation log it gives on Raspbian Wheezy:
$ sudo apt-get install postgresql-9.1
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libpq5 lsb-release postgresql-client-9.1 postgresql-client-common postgresql-common ssl-cert
Suggested packages:
  lsb oidentd ident-server locales-all postgresql-doc-9.1 openssl-blacklist
The following NEW packages will be installed:
  libpq5 lsb-release postgresql-9.1 postgresql-client-9.1 postgresql-client-common postgresql-common ssl-cert
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 5444 kB of archives.
After this operation, 22,6 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libpq5 armhf 9.1.11-0wheezy1 [520 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ wheezy/main lsb-release all 4.1+Debian8+rpi1 [27,1 kB]
Get:3 http://mirrordirector.raspbian.org/raspbian/ wheezy/main postgresql-client-common all 134wheezy4 [63,3 kB]
Get:4 http://mirrordirector.raspbian.org/raspbian/ wheezy/main postgresql-client-9.1 armhf 9.1.11-0wheezy1 [1337 kB]
Get:5 http://mirrordirector.raspbian.org/raspbian/ wheezy/main ssl-cert all 1.0.32 [19,5 kB]
Get:6 http://mirrordirector.raspbian.org/raspbian/ wheezy/main postgresql-common all 134wheezy4 [138 kB]
Get:7 http://mirrordirector.raspbian.org/raspbian/ wheezy/main postgresql-9.1 armhf 9.1.11-0wheezy1 [3339 kB]
Fetched 5444 kB in 6s (848 kB/s)                                                                                                                            
Preconfiguring packages ...
Selecting previously unselected package libpq5.
(Reading database ... 61782 files and directories currently installed.)
Unpacking libpq5 (from .../libpq5_9.1.11-0wheezy1_armhf.deb) ...
Selecting previously unselected package lsb-release.
Unpacking lsb-release (from .../lsb-release_4.1+Debian8+rpi1_all.deb) ...
Selecting previously unselected package postgresql-client-common.
Unpacking postgresql-client-common (from .../postgresql-client-common_134wheezy4_all.deb) ...
Selecting previously unselected package postgresql-client-9.1.
Unpacking postgresql-client-9.1 (from .../postgresql-client-9.1_9.1.11-0wheezy1_armhf.deb) ...
Selecting previously unselected package ssl-cert.
Unpacking ssl-cert (from .../ssl-cert_1.0.32_all.deb) ...
Selecting previously unselected package postgresql-common.
Unpacking postgresql-common (from .../postgresql-common_134wheezy4_all.deb) ...
Adding 'diversion of /usr/bin/pg_config to /usr/bin/pg_config.libpq-dev by postgresql-common'
Selecting previously unselected package postgresql-9.1.
Unpacking postgresql-9.1 (from .../postgresql-9.1_9.1.11-0wheezy1_armhf.deb) ...
Processing triggers for man-db ...
Setting up libpq5 (9.1.11-0wheezy1) ...
Setting up lsb-release (4.1+Debian8+rpi1) ...
Setting up postgresql-client-common (134wheezy4) ...
Setting up postgresql-client-9.1 (9.1.11-0wheezy1) ...
update-alternatives: using /usr/share/postgresql/9.1/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode
Setting up ssl-cert (1.0.32) ...
Setting up postgresql-common (134wheezy4) ...
Adding user postgres to group ssl-cert
Building PostgreSQL dictionaries from installed myspell/hunspell packages...
Setting up postgresql-9.1 (9.1.11-0wheezy1) ...
Creating new cluster (configuration: /etc/postgresql/9.1/main, data: /var/lib/postgresql/9.1/main)...
Moving configuration file /var/lib/postgresql/9.1/main/postgresql.conf to /etc/postgresql/9.1/main...
Moving configuration file /var/lib/postgresql/9.1/main/pg_hba.conf to /etc/postgresql/9.1/main...
Moving configuration file /var/lib/postgresql/9.1/main/pg_ident.conf to /etc/postgresql/9.1/main...
Configuring postgresql.conf to use port 5432...
update-alternatives: using /usr/share/postgresql/9.1/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode
[ ok ] Starting PostgreSQL 9.1 database server: main. 
The log gives a lot of initial information, and most importantly you can see that after issuing the command and it completes, you already have a running database server. Additional proof of this the output of the ps command mentioned before:
$ ps aux | grep postgres

postgres  4303  1.8  1.9  47400  7420 ?        S    01:24   0:05 /usr/lib/postgresql/9.1/bin/postgres -D /var/lib/postgresql/9.1/main -c config_file=/etc/postgresql/9.1/main/postgresql.conf

postgres  4311  0.0  0.4  47400  1712 ?        Ss   01:24   0:00 postgres: writer process                                                                                                    
postgres  4312  0.0  0.3  47400  1528 ?        Ss   01:24   0:00 postgres: wal writer process                                                                                                
postgres  4313  0.0  0.6  47820  2424 ?        Ss   01:24   0:00 postgres: autovacuum launcher process                                                                                       
postgres  4314  0.0  0.3  17496  1460 ?        Ss   01:24   0:00 postgres: stats collector process                                                                                           
pi        4350  0.0  0.2   5244   836 pts/0    S+   01:29   0:00 grep --color=auto postgres
Alternatively, if you want the latest and greatest edition (currently this is version 9.3.1 - nearly two months old now), you'll have to try and build it from sources. The option for adding the PostgreSQL's own repositories is not viable, because there are no packages in there dedicated to the ARM architecture.

    Next is the initial administration
Currently, the only user known to the database is the default superuser named postgres. The specific thing about this user is that its password is literally unknown in the beginning. So it, at least, must be set for the sake of simple security. This is relatively easy process consisting in two basic steps.

As a privileged user of the operating system (with the sudo command), you have to open this very important configuration file - /etc/postgresql/9.1/main/pg_hba.conf. The piece of text is the ultimate conveyor of who and how will connect to your database. Rules are clear and simple, but strong and provide for efficient security on the network level. I won't go in much more detail about its contents, for there already are pretty good sources of information (like this one). In short the following line:
local   all   postgres   peer
have to be changed to this:
local   all   postgres   trust
This gives you a complete password-less access to the superuser from the localhost for the time being.

The second step is to set the password. The following log shows the necessary commands and the the output:
$ sudo -u postgres psql postgres
[sudo] password for user: psql (9.3.2)Type "help" for help.
postgres=# \password postgres Enter new password: Enter it again: postgres=# \q

As a final sub-step and to keep things secure, the line in the configuration file is good to be changed again. The word trust that we introduced, can be changed to md5. This means that the access to the superuser will be allowed only with password, even on the localhost.

In order for these changes to be applied, the configuration have to be reloaded (no need to restarting PostgreSQL server, when you've changed only the configuration). From the several possible methods, the following worked fine for me:
 $ sudo service postgresql reload
 * Reloading PostgreSQL 9.1 database server                    [ OK ]
Further on things become quite specific with the different types of host access, graphical and command line clients, etc. And it's all up to you.

Comments

Popular posts from this blog

Book on How to Cluster some Pis with Hadoop

To be honest and straightforward I expected more from a book with title like Raspberry Pi Super Cluster . The author Andrew K. Denis has a very clear vision on the subject (like in his previous book Raspberry Pi Home Automation with Arduino , which I liked a lot) . He's done his best to deliver an exhaustive set-up while being concise at the same time, but it seems to me, this clearly is the wrong format for a book on the given topic. Stack Pis for parallel power Now having this book at hand, I finally got the chance to answer many of the questions I had about clustering, and how it can be applied to a set of Raspberry Pis. The first impression is that it is very well structured and gradual. Lets see, the first two chapters are short introductions to parallel computing (background history and the contemporary systems) and the initial set-up respectively. They're short and to the point. And that's the way it should be - it is presumed that if you're going paralle

Java 8 on the Raspberry Pi

This topic being approached exhaustively may become vast and is fit for at least a book. I'll have to keep it short and concise here, so I'll stick to a few key points: Java Runtime vs JDK - actually there is no discussion here - if you you intend to run programming projects you need the development kit, period. (It contains the runtime anyway.) Java 7  vs Java 8 (JDKs) - this could require some debate. Java 7 is the mature and default option to go with. Having around two years in production, it is the safer choice. Java 8 has been just released, and its shortcomings are still unknown. On the other hand Java 8 has numerous improvements to the language, and Oracle wouldn't approve it for release if it wasn't quite well tested. Another facet to be considered is that Java 7 is well presented in the repositories, while currently Java 8 have to be downloaded, installed and maintained (the regular updates - mostly for security reasons) all manually. Source examples - ne