During the time of this article, the instructions on the Ubuntu wiki were pretty light and from what I can see not quite complete. These instructions are currently written for PostgreSQL 8.x. and 9.x.
Installing PostgreSQL
Ensure UTF-8 Encoding is Set
If you did not follow the Bonsai Framework to setup Ubuntu yourself, make sure that your operating system has the right encoding set as explained in the "Select Locale" part of my Ubuntu Linux Base Server article.
Install
The Ubuntu install is simply,
sudo apt-get install postgresql # installs postgresql
Similar to how Apache is installed, Ubuntu has it's own standard. Here are the keynotes.
You will now have a postgresql user on your system. This account is reserved for running Postgresql in a secure manner. This account neither has a password or a home directory. This account also has a role in the database for administration purposes. I will show you how to use this account as we go forward.
The postgresql service is also added to the system to start automatically.
Finally here are some directories to be aware of,
- /var/lib/postgresql/8.4/main - location of the main database that can only be viewed and modified by the user postgresql.
- /etc/postgresql/8.4/main - location of all configuration files.
- /var/log/postgresql/ - location where Ubuntu keeps all the logs and automatically is set to archive and roll over.
That's all there is for the installation. Ubuntu apt-get makes it easy and does all the hard work for you.
Secure Master Account
By default, the master account in the database postgres does not have a password. We want to set this up so that we can provide access to a dba without the dba requiring sudo access to the operating system. Also, leaving at is makes postgres open to any user with sudo privileges.
Change the Default postgres Database User Account
The first step, is to launch the postgresql shell.
sudo -u postgres psql postgres
The command is confusing so here is a break down,
sudo -u postgres # Run as the operating system user postgres which also has a role in the database. psql # Launch the postgres shell. postgres # Name of the database to load (not the user).
Sometimes the last parameter is not used. This is short-hand. If no database name is specified, psql will try to load the database matching the user name.
Here is what it looks like once you are in,
psql (8.4.1) Type "help" for help. postgres=#
All commands are prefixed by the the \. So to change the postgres account password. In the Bonsai Framework, we use the selected password algorithm derived from the server name.
postgres=# \password postgres
Note that this changes the account inside of postgres, not the operating system account postgres. Finally exit the postgres shell,
postgres=# \quit
With this change you can now use a GUI client software such as pgAdmin III. Note that be by default, postgres is very secure. As such with this change you will be able to use pgAdmin III in only the following 2 scenarios,
- Using the pgAdmin III installed on the same system as Postgresql.
- From another machine using an SSH Tunnel.
Further Reading
1 Comment
Roderick Fong Yee
There seems to be a problem with idle postgres connections that sit around and are not terminated. Some sources say that using postgres 8.3 and below to use pgbouncer but postgres 9 there is a query that can be used. I am curious to find out if this is the reason why confluence has been crashing though I still have the dreaded context root memory leak that presents itself after a java crash.
Source - http://stackoverflow.com/questions/13236160/is-there-a-timeout-for-postgresql-connections