Update on 25/Jun/2015
We have added the links to the compiled deb packages.
Skip to this section if you only want to download and install them.
This article is an updated version of our previous post where we explained how to install PostgreSQL on the Raspberry Pi.
Many exciting things have happened since then, but the main ones are that PostgreSQL is now at version 9.4 with plenty of improvements and, finally, a new model of our beloved credit-card-sized computer, the Raspberry Pi 2, was released.
Now, let’s cut to the chase and start installing PostgreSQL on it. Just to make things easier, the whole procedure listed here is supposed to work on both models of the Raspberry Pi (1 and 2).
The first thing you have to do is choose which version of PostgreSQL to install and this is pretty straight forward: to compile or not to compile.
Well, on the current Raspbian Linux release (2015-05-05), which is the distribution recommended by the Raspberry Pi Foundation and the one we are using for this process, the only PostgreSQL present on the Raspbian repository is the version 9.1. If you dont mind about using a previous version, you can install it as you install any other Debian package through apt-get.
But, if you want the latest version of PostgreSQL, which is the 9.4 at the time we are writing this, you have to downloaded it from the PostgreSQL Apt Repository and compile it. Don’t worry, this sounds harder than it actually is and we have you covered.
To help you decide, the recommended PostgreSQL version to be used is always the latest, due to various improvements and new features included in every new release.
The installation details of both options are listed below:
Go to the terminal and type the command:
sudo apt-get install postgresql-9.1
That’s it. No, seriously, you are done! But then, what was the fun on that?
As the previous method, open a terminal and type the following commands. You can also copy-and-paste it to avoid any typing mistakes.
- Add the PostgreSQL Apt Repository (source)
echo "deb-src http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
- Import the repository signing key
wget -qO - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
- Update the package list and upgrade the system
sudo apt-get update sudo apt-get upgrade
- Install necessary tools
sudo apt-get install build-essential fakeroot
- Install all the dependencies needed
sudo apt-get build-dep postgresql-9.4 sudo apt-get build-dep postgresql-common sudo apt-get build-dep postgresql-client-common sudo apt-get build-dep pgdg-keyring
Do not run the compilation as root. The second line, which compiles postgresql-9.4, will take a long time to finish, so, it is recommended to use a remote session with screen or tmux. If you don’t know how to use them, just repeat the instruction in case any disconnection happens.
cd /tmp apt-get source --compile postgresql-9.4 apt-get source --compile postgresql-common apt-get source --compile postgresql-client-common apt-get source --compile pgdg-keyring
- Create local repository
sudo mkdir /var/local/repository echo "deb [ trusted=yes ] file:///var/local/repository ./" | sudo tee /etc/apt/sources.list.d/my_own_repo.list cd /var/local/repository sudo mv /tmp/*.deb . dpkg-scanpackages ./ | sudo tee Packages > /dev/null && sudo gzip -f Packages
- Update package list
sudo apt-get update
- Finally, PostgreSQL-9.4 can be installed
sudo apt-get install postgresql-9.4
Feeling good? It was not that hard, was it? Now you have the latest version of the world’s most advanced open source database installed and ready to fly on your Raspberry Pi.
Ok, now that you know how to compile PostgreSQL and install it by yourself, I can show you a shortcut. Don’t get me wrong, the knowledge you have acquired compiling PostgreSQL by hand is well worth, but if you don’t want to wait for the compilation to finish, we have already compiled the packages for you and uploaded them to our Dropbox folder.
Just to be clear, if you want to download our compiled packages, you don’t have to follow any of the instructions explained on the previous sections. Could I have told you this before? Yes, of course I could, but then you wouldn’t have learned how to compile PostgreSQL, would you?
You just have to type the following in the terminal:
sudo mkdir /var/local/repository cd /var/local/repository sudo wget -O postgresql-9.4.4-raspbian.tgz https://www.dropbox.com/s/t9x78hbfo2mb8yi/postgresql-9.4.4-raspbian.tgz?dl=1 sudo tar -xvzf postgresql-9.4.4-raspbian.tgz echo "deb [ trusted=yes ] file:///var/local/repository ./" | sudo tee /etc/apt/sources.list.d/my_own_repo.list sudo apt-get update sudo apt-get install postgresql-9.4
Done! We will update the Dropbox link on future PostgreSQL releases.
Independently of which method you choose, during the installation the user “postgres” is created with its home directory located in /var/lib/postgresql/. Usually on Raspbian Linux (the same for Debian and Ubuntu) PostgreSQL configuration files are stored in /etc/postgresql/9.4/main/, and data directory is stored in /usr/local/var/postgres/. Now it is possible to start/stop/restart the postmaster (the main process of a PostgreSQL server) using the command:
sudo service postgresql start|stop|restart
While tweaking the configuration file parameters, keep in mind that most of the changes should require a postmaster restart.
Thanks for all your comments and questions sent on the previous article. We hope this updated version solves the problems you were having to install PostgreSQL and, if it doesn’t, feel free to comment and send new questions here as well.
Last updated 2015-06-17 13:49:42 CEST