Steven Erat's Blog Steven Erat Photography
 
 
Viewing By Entry
 
 

TalkingTree  A shortcut to getting started with PostgreSQL database on Linux

 

Recently I've needed to install the PostgreSQL 8.14 database server on a couple Linux machines for testing. Here is some information and scripts to make it easier for you start and stop the database, since installing from source instead of RPM leaves you without the convenient /etc/init.d boot scripts for Postgres and requires you to start Postgres database with the postmaster command when su'd as the postgres user. A bit of a headache... so I wrote the script shown further below as a convenience when managing Postgres and it may be helpful for those who don't want to read all the docs right away.

After having downloaded and uncompressed (tar -xvzf postgresql-8.1.4.tar.gz), the installation instructions begin with the a short version, to be run from inside the uncompressed source directory. I've modified the short version such that when creating the system postgres user account, no shell is given for the postgres user, then later a shell can be specified when using the su command to run the postmaster (The postmaster command can not be run by root directly).

./configure
gmake
su
gmake install
adduser postgres -s /sbin/nologin
mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data
su -s /bin/sh postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
/usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data >logfile 2>&1 &

You may wish to create a database and assign a specific user:

createuser cfuser
createdb mydatabase -U cfuser

You may also wish to connect to Postgres over TCP from another host. To make Postgres listen on a TCP socket (default port 5432), start the postmaster with the -i option. To allow access from other hosts, you'll need to edit the file {postgres_home}/data/pg_hba.conf, for which you can find the documentation here. Several networks can be added to the authorization list such as this example:

# TYPE DATABASE USER CIDR-ADDRESS METHOD

# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 10.1.0.0/16 trust
host all all 192.168.1.0/24 trust

Installing from source means that you must start and stop the database by su'ing to the postgres user and running the postmaster to start it or pg_ctl to stop it. Further, when rebooting the machine, the Postgres database won't start up automatically as it would if it had been installed from the RPM (Postgres server RPM is not available from the download page).

To make my life easier when managing these Postgres databases, I've created the following pgsqld script which can be copied to the {postgres_home}/bin/ directory:

#!/bin/sh

# chkconfig: 345 89 13
# description: starts the PostgreSQL database server
# author: Steven Erat (serat -/at/- adobe -/dot/- com)
# caveat emptor: This script is released without warranty and
   and is not guaranteed to work in all cases.
   The user assumes all liability for using this script.

ID=`id -u`

if [ ! $ID -eq 0 ]; then
   echo "You must be root to start or stop PostgreSQL database server"
   exit 1
fi

PGHOME=/usr/local/pgsql
PGLOG=$PGHOME/logs/pglog

pgstart() {
   
   pglisten=`netstat -an | grep PGSQL | cut -d'.' -f4`
   
   if [ ! "$pglisten" = "" ]; then
    echo " PostgreSQL database is already running and listening on port $pglisten "
    exit 1
   fi

   echo " Starting the PostgreSQL database server ..."

   su -s /bin/sh postgres -c "$PGHOME/bin/postmaster -D $PGHOME/data -i >> $PGLOG 2>&1 &"
   
   sleep 5
   
   pglisten=`netstat -an | grep PGSQL | cut -d'.' -f4`
   
   if [ -n $pglisten ]; then
    echo " PostgreSQL database is listening on port $pglisten "
   fi
}

pgstop() {

   
   pglisten=`netstat -an | grep PGSQL | cut -d'.' -f4`
   
   if [ -z "$pglisten" ]; then
       echo " PostgreSQL isn't running "
   else
      echo " Stopping the PostgreSQL database server that is listening on port $pglisten ..."
   
      su -s /bin/sh postgres -c "$PGHOME/bin/pg_ctl stop -D $PGHOME/data -m fast >> $PGLOG 2>&1 &"
      
      sleep 4
   
      pglisten=`netstat -an | grep PGSQL | cut -d'.' -f4`


   if [ ! "$pglisten" = "" ]; then
    echo " !!! PostgreSQL database has NOT been stopped !!!"
   else
    echo " PostgreSQL database has been stopped! "
       fi
   fi

}

pgstatus() {
   
   echo " `su -s /bin/sh postgres -c "$PGHOME/bin/postmaster --version"`"
   
   pglisten=`netstat -an | grep PGSQL | cut -d'.' -f4`

if [ -z "$pglisten" ]; then
   echo " PostgreSQL isn't running "
else
   echo " PostgreSQL database server is running and listening on port $pglisten"
fi
   
   echo " See $PGLOG for PostgreSQL logging"
}

ARG=$1

case $ARG in
   
   start)
      pgstart
   ;;

   stop)
   
      pgstop
   ;;

   restart)
      
      pgstop
      pgstart
   ;;

   status)
   
      pgstatus
   ;;

   *)   
   
      echo " Usage:$0 (start|stop|restart|status)"
   ;;

esac

exit 0

To use the script, you can make it executable and then run it with the arguments of start, stop, restart, or status (i.e. ./pgsqld start).

To make the database start on a system boot or to add the convenience of controlling the database with the service command (i.e. service pgsqld start), make a symbolic link to the pgsqld script from /etc/init.d, then run chkconfig to add it as a service:

cd /etc/init.d
ln -s /usr/local/pgsql/bin/pgsqld pgsqld
chkconfig --add pgsqld
chkconfig --list pgsqld
pgsqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off

Postgres will now start after a reboot, and from any system directory root can use the service command to control the database:

[root@RHEL4QA1 ~]# service pgsqld start
Starting the PostgreSQL database server ...
PostgreSQL database is listening on port 5432
[root@RHEL4QA1 ~]# service pgsqld status
postmaster (PostgreSQL) 8.1.4
PostgreSQL database server is running and listening on port 5432
See /usr/local/pgsql/logs/pglog for PostgreSQL logging
[root@RHEL4QA1 ~]# service pgsqld stop
Stopping the PostgreSQL database server that is listening on port 5432 ...
PostgreSQL database has been stopped!
[root@RHEL4QA1 ~]# service pgsqld status
postmaster (PostgreSQL) 8.1.4
PostgreSQL isn't running
See /usr/local/pgsql/logs/pglog for PostgreSQL logging

 


Comments

Interesting script.... you might want to check out the pg_ctl program as well. Also I should mention that while using -i on the command line will work for allowing tcp/ip access, the recommended procedure is to modify listen_addresses in the postgresql.conf file. HTH,


Wow, thanks for the tip on the postgresql.conf file.


For help with installing/running Postgres on Mac OS X, see Jared's blog entry here:
http://www.web-relevant.com/blogs/cfobjective/inde...


I used you script and it seem to work well but I got a different output. Mine was:
pgsqld 0:off 1:off 2:off 3:on 4:on 5:on 6:off


Can you explain why that might be. I actually don't know what each of those are.

TIA, Case


 

 

Calendar

 
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Search This Site

 
This is an exact search only

About This Site

 
I live west of Boston and work as a Software Engineer with ColdFusion and Flex, specializing in Linux. Recently I graduated in Professional Digital Photography from CDIA.
More about me

Recent Entries

 
A ColdFusion Trick for Lost D..
Starting ColdFusion9 Solr: Us..
Adobe LiveCycle DataServices ..

Recent Comments

 
Posted By Aaron Longnion:
Thanks Steven, I just ran into this problem, remembered your tweet about it, and found your blog on it. :)

Posted By srinyvas:
Hai, This information is very useful and i like your excellent writing skill. Can i copy this Content to my website top management colleges ...

Posted By Steven Erat:
@Wade - Glad I could help! Thanks for letting me know it worked for you too.

recently played

 
The Candid Frame #70 - Greg Gorman
by Ibarionex R. Perello
on The Candid Frame: A Photography Podcast

now playing, a plug-in for itunes

Categories

 
RSS Adobe (34)
RSS Bicycling (9)
RSS Blogging (39)
RSS Books (13)
RSS Breeze (13)
RSS CFMX Podcasts (10)
RSS ColdFusion (427)
RSS Computer Technology (51)
RSS Events (26)
RSS Flex (20)
RSS Gadgets (10)
RSS HiTech Industry (16)
RSS Java (25)
RSS Learning (57)
RSS Linux (70)
RSS Mac OS X (22)
RSS Macromedia (27)
RSS Meetup (35)
RSS New England (62)
RSS Odds & Ends (25)
RSS Outdoors (32)
RSS Personal (29)
RSS Photography (111)
RSS Photoshop (29)
RSS Podcasts (18)
RSS Rants (19)
RSS Restaurants (8)
RSS Science (34)
RSS Spain (16)
RSS Travel (42)
RSS Twitter (10)
RSS Video (20)
RSS Webcam (3)
RSS Writing (10)

Blogs I Read

 
Terrence Ryan
Ben Forta
Ray Camden
Kinky Solutions
Dan Vega
Gary Gilbert
Simeon Bateman
Red Hat Blogs
O'Reilly Digital Media
O'Reilly Radar
John Nack
The Strobist
Scott Kelby
Matt Kloskowski
Joe McNally
Digital Photography School
Engadget
Science Blog

RSS

 


Add to Google
Add to My Yahoo!

Aggregated By

 


Consumed By Feed-Squirrel.com
Aggregated by ColdFusionBlogger.org

Credits and Stuff

 
BlogCFC - Free ColdFusion Powered Blog Software
CJM Group - ColdFusion Website Hosting


 
 
blog | photos | flickr | referers | webcam | stats | about | contact
 
Copyright © 2010 Steven Erat. All rights reserved.
This is a personal weblog. The opinions expressed here represent my own and not those of my employer