switcherswitcherswitcherswitcher

User login

Who's online

There are currently 1 user and 2 guests online.

Online users

  • tomas_ramsi

Who's new

  • tomas_ramsi
  • zhimo
  • amandeepkaur
  • hsbernauer
  • daniloceschin

RSS Feeds

Technorati

PostgreSQL on Ubuntu Linux - How To

This is a repost and update of an old blog entry on one of my personal sites. I'm planning on cleaning out the site sometime soon, so I thought this would make a good new home for the tutorial...

This quick walk-through will help you to install the PostgreSQL database server and the PgAdmin administration application on Ubuntu Linux, and also set up the server so it allows access to other PC's on your network.

Before we move on, this guide was tested on (and intended for) the current release of Ubuntu Linux, (6.10 - Edgy Eft) and PostgreSQL 8.2, but it should also be applicable to older versions (of Ubuntu and PostgreSQL) and other Debian based distros. (Update: This also works fine for PostgreSQL 8.2 on the newer version of Ubuntu 7.04 - Feisty, you just shouldn't need the backports mentioned below).

First off, PostgreSQL 8.2 isn't in the main repositories in Edgy, you'll need to have backports enabled to get hold of the latest packages. Once you've done that, let's move on.

Right, now let's install the database server. At the command-line, enter the following (Or you can do all this in Synaptic - just search for and install the packages listed in the commands):

daz@na-box:~$ sudo apt-get install postgresql-8.2 postgresql-client-8.2
daz@na-box:~$ sudo apt-get install pgadmin3 pgadmin3-data

This installs the database server, and the pgAdmin administration application (If you don't really get on with the pgAdmin GUI, there is an alternative in the form of phpPgAdmin - a web-based administration interface - a guide to setting this up can be found here).

Now we need to reset the password for the 'postgres' admin account for the server, so we can use this for all of the system administration tasks. Type the following at the command-line (substitute in the password you want to use for your administrator account):

daz@na-box:~$ sudo su postgres -c psql template1
template1=# ALTER USER postgres WITH PASSWORD 'password';
template1=# \q

Then, from here on in we can use pgAdmin to run the database server. To get a menu entry for pgAdmin do the following...

daz@na-box:~$ sudo gedit /usr/share/applications/pgadmin.desktop

Then paste the following into the file:

[Desktop Entry] Comment= PostgreSQL Administrator III
Name=pgAdmin III
Encoding=UTF-8
Exec=pgadmin3
Terminal=false
Comment[en_GB]=PostgreSQL Administrator III
Icon=/usr/share/pixmaps/pgadmin3.xpm
Type=Application
Categories=GNOME;Application;Database;System;
Name[en_GB]=pgAdmin III

Then save the file and exit gedit. You should find the launcher in the System Tools section of the Applications menu. Alternatively, you could just type 'pgadmin3' at the shell. The wizards to connect to the database should be pretty simple to figure out.

Finally, we need to open up the server so that we can access and use it remotely - unless you only want to access the database on the local machine. To do this, first, we need to edit the postgresql.conf file:

daz@na-box:~$ sudo gedit /etc/postgresql/8.2/main/postgresql.conf

Now, to edit a couple of lines in the 'Connections and Authentication' section...

Change the line:

#listen_addresses = 'localhost'

to

listen_addresses = '*'

and also change the line:

#password_encryption = on

to

password_encryption = on

Then save the file and close gedit.

Now for the final step, we must define who can access the server. This is all done using the pg_hba.conf file.

daz@na-box:~$ sudo gedit /etc/postgresql/8.2/main/pg_hba.conf

Comment out, or delete the current contents of the file, then add this text to the bottom of the file:

# DO NOT DISABLE!
# If you change this first entry you will need to make sure that the
# database
# super user can access the database using some other method.
# Noninteractive
# access to all databases is required during automatic maintenance
# (autovacuum, daily cronjob, replication, and similar tasks).
#
# Database administrative login by UNIX sockets
local   all         postgres                          ident sameuser

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD

# "local" is for Unix domain socket connections only
local   all         all                               md5
# IPv4 local connections:
host    all         all         127.0.0.1/32          md5
# IPv6 local connections:
host    all         all         ::1/128               md5

# Connections for all PCs on the subnet
#
# TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
host    all         all         [ip address]          [subnet mask]          md5

and in the last line, add in your subnet mask (i.e. 255.255.255.0) and the IP address of the machine that you would like to access your server (i.e. 138.250.192.115). However, if you would like to enable access to a range of IP addresses, just substitute the last number for a zero and all machines within that range will be allowed access (i.e. 138.250.192.0 would allow all machines with an IP address 138.250.192.x to use the database server).

That's it, now all you have to do is restart the server and all should be working!

daz@na-box:~$ sudo /etc/init.d/postgresql-8.2 restart

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Submitted by Daz on February 26, 2007 - 5:00pm.

If you wish to use the command-line tool psql with the 'postgres' account, this is not possible unless you create a full Unix user account called postgres, however, you can still log into 'psql' without doing this... Here's both ways of doing this:

Method 1 - Generating the 'postgres' account

Type the following at the command-line:

daz@na-box:~$ sudo passwd -d postgres

This clears the password for the Unix account 'postgres'. Now type the following and give it a password (preferably the same as you set in the above tutorial):

daz@na-box:~$ sudo su postgres -c passwd

Now all you have to do to log into psql as the 'postgres' user is type the following:

daz@na-box:~$ su postgres
postgres@na-box:/home/daz$ psql

Method 2 - The Quick Way...

When you want to access psql as the 'postgres' user, type the following:

daz@na-box:~$ sudo su postgres -c psql
Submitted by Daz on February 26, 2007 - 4:59pm.

An alternative to using the 'postgres' account with your database, would be to create a new user account with admin permissions. So, say I wanted to make my user account (daz) an administrator, all i'd have to do is the following:

daz@na-box:~$ sudo su postgres -c createuser daz

Then you have to give this new user role a name (I called it daz), and then say 'y' to the question "Shall the new role be a superuser?"

Finally, you must give your new account a password. This is done via the following (substitute your username and password):

daz@na-box:~$ sudo su postgres -c psql
postgres=# ALTER USER daz WITH PASSWORD 'mypassword';
ALTER ROLE
postgres=# \q

Now you can log into PostgreSQL as an administrator with your own account! :)

Submitted by dented (not verified) on September 13, 2007 - 7:59am.

Hey there,

Just thought I'd add that when creating a new admin user it wouldn't let me log in until I had created a Database for with the same name as the new user. I've been using postgresql for all of 10 minutes, so I'm still poking around in psql trying to find a way to change the default db for my new user.

Since I've only got ssh-access from a windows-box to my linuxmachine and I don't want to muck around with Xing, it's commandline all the way. =)

Submitted by Paritosh (not verified) on June 14, 2007 - 11:32pm.

Great HowTo. Thanks!!

Submitted by Anonymous (not verified) on August 3, 2007 - 8:51am.

Helpful tutorial. Thank you.
My Linux distro: Ubuntu edgy1 (Pioneer Migration Server 2.1 really, from Technalign Inc.)
Being new to Ubuntu/Debian I fumbled to resolve depends. Thanks to the following command which installed entire Postgres 8.2 package.

sudo apt-get -f install

Before this command, I tried these;

sudo apt-get -f install gforge-common
sudo apt-get install postgresql postgresql-client
sudo apt-get install postgresql-8.2 postgresql-client-8.2

but none of these were installed and missing depends. were always reported. Then the sudo apt-get -f install automatically installed everything.

Submitted by Daz on August 3, 2007 - 9:56am.

That's probably not the best thing to do if you're having problems installing an application...

The '-f' option means 'install by force'. Yes, it has installed your packages, but it is highly unlikely that they will work as they are missing some other packages that they depend on to function (hence the term 'dependancies' - these were not installed by using '-f', they were ignored). The '-f' option makes apt-get ignore all problems during the install and just puts the packages on the system blindly - unfortunately, 9 times out of 10, this will not work (yes the package will be installed, but it will not function correctly).

Your best bet would be to find a way to install the dependant packages that it was asking for in the first place, and then reinstall PostgreSQL. I wouldn't trust putting any data into an install done the way you have suggested.

Submitted by mukesh_bhakta (not verified) on September 12, 2007 - 2:52am.

Hi guys,

Just thought I should make all guys working on PG 8.2 under Ubuntu aware of the following -
1. Please avoid referring to articles written for PG 8.1 where they tell you to modify the pg_hba.conf file. It is not necessary for 8.2 as most of the hardwork has already been done by the good guys.
2. Install the latest version of PGAdmin (1.6.3 at the time of writing this mail) and if anything is at fault PGAdmin will tell you what to do. In my case it told me to 'only' add the following line -

host all all 192.168.0.0/24 md5

1.6.2 or earlier versions don't have the self-diagnosis feature in-built.

Cheers

MB

Submitted by Daz on September 12, 2007 - 1:41pm.
mukesh_bhakta wrote:

Please avoid referring to articles written for PG 8.1 where they tell you to modify the pg_hba.conf file.

Does this mean that this article is outdated (as it still works)? The only change made to the 'pg_hba.conf' file in the above instructions is the one that you (or rather PGAdmin) points out (the bottom line to allow access from remote machines) - it was just suggested to replace the entire file as this prevents any problems occurring when someone misreads the instructions and edits the wrong part of the file.

mukesh_bhakta wrote:

Install the latest version of PGAdmin (1.6.3 at the time of writing this mail) and if anything is at fault PGAdmin will tell you what to do.

Neat new feature though! Does it do the editing of the file for you, or do you still have to do that yourself?

Submitted by Anonymous (not verified) on September 27, 2007 - 10:24am.

Great tutorial. Thanks a lot!

Submitted by Anonymous (not verified) on November 21, 2007 - 10:37pm.

Excellent guide, thanks a million...spent ages trying to figure out how to access the DB server with the default login!