5 # Configure postgresql database for GForge
10 if [ $(id -u) != 0 ] ; then
11 echo "You must be root to run this, please enter passwd"
24 echo "Usage: $0 {configure-files|configure|purge|purge-files|dump|restore}"
28 # Tell PostgreSQL to let us use the database
29 db_passwd=$(grep ^db_password= /etc/gforge/gforge.conf | cut -d= -f2-)
30 ip_address=$(grep ^ip_address= /etc/gforge/gforge.conf | cut -d= -f2-)
31 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
32 db_user=$(grep ^db_user= /etc/gforge/gforge.conf | cut -d= -f2-)
33 db_host=$(grep ^db_host= /etc/gforge/gforge.conf | cut -d= -f2-)
34 pattern=$(basename $0).XXXXXX
35 pg_version=$(dpkg -s postgresql | awk '/^Version: / { print $2 }')
36 if [ "$db_host" == "127.0.0.1" -o "$db_host" == "localhost" ]
38 # Otherwise the line wouldn't be used
39 # And postgres auth would fail
42 if dpkg --compare-versions $pg_version lt 7.3 ; then
43 # PostgreSQL configuration for versions prior to 7.3
44 echo "Configuring for PostgreSQL 7.2"
45 cp -a /etc/postgresql/pg_hba.conf /etc/postgresql/pg_hba.conf.gforge-new
46 # if previous string, else no previous string
47 if grep -q "^host.*gforge_passwd$" /etc/postgresql/pg_hba.conf.gforge-new ; then
48 perl -pi -e "s/^host.*gforge_passwd$/host $db_name $ip_address 255.255.255.255 password gforge_passwd/" /etc/postgresql/pg_hba.conf.gforge-new
50 cur=$(mktemp /tmp/$pattern)
51 echo "### Next line inserted by GForge install" > $cur
52 echo "host $db_name $ip_address 255.255.255.255 password gforge_passwd" >> $cur
53 cat /etc/postgresql/pg_hba.conf.gforge-new >> $cur
54 cat $cur > /etc/postgresql/pg_hba.conf.gforge-new
57 su -s /bin/sh postgres -c "touch /var/lib/postgres/data/gforge_passwd"
58 su -s /bin/sh postgres -c "/usr/lib/postgresql/bin/pg_passwd /var/lib/postgres/data/gforge_passwd > /dev/null" <<-EOF
64 # PostgreSQL configuration for versions from 7.3 on
65 echo "Configuring for PostgreSQL 7.3"
66 cp -a /etc/postgresql/pg_hba.conf /etc/postgresql/pg_hba.conf.gforge-new
67 # if 7.2 formatted string, elseif 7.3 string, else no previous string
68 if grep -q "^host.*gforge_passwd$" /etc/postgresql/pg_hba.conf.gforge-new ; then
69 perl -pi -e "s/^host.*gforge_passwd$/host $db_name $db_user $ip_address 255.255.255.255 password/" /etc/postgresql/pg_hba.conf.gforge-new
70 # the below could cause issues if gforge's line isnt at the top of the file, but its
71 # the only way to allow changing of the db_name.
72 elif grep -q "^host.*password$" /etc/postgresql/pg_hba.conf.gforge-new ; then
73 perl -pi -e "s/^host.*password$/host $db_name $db_user $ip_address 255.255.255.255 password/" /etc/postgresql/pg_hba.conf.gforge-new
75 cur=$(mktemp /tmp/$pattern)
76 echo "### Next line inserted by GForge install" > $cur
77 echo "host $db_name $db_user $ip_address 255.255.255.255 password" >> $cur
78 cat /etc/postgresql/pg_hba.conf.gforge-new >> $cur
79 cat $cur > /etc/postgresql/pg_hba.conf.gforge-new
82 # Remove old password file, created by 7.2, not used by 7.3
83 if [ -e /var/lib/postgres/data/gforge_passwd ] ; then
84 rm -f /var/lib/postgres/data/gforge_passwd
90 # Create the appropriate database user
91 pg_version=$(dpkg -s postgresql | awk '/^Version: / { print $2 }')
92 db_passwd=$(grep ^db_password= /etc/gforge/gforge.conf | cut -d= -f2-)
93 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
94 db_user=$(grep ^db_user= /etc/gforge/gforge.conf | cut -d= -f2-)
95 pattern=$(basename $0).XXXXXX
96 tmp1=$(mktemp /tmp/$pattern)
97 tmp2=$(mktemp /tmp/$pattern)
98 if dpkg --compare-versions $pg_version lt 7.3 ; then
99 if su -s /bin/sh postgres -c "createuser --no-createdb --no-adduser $db_user" 1> $tmp1 2> $tmp2 \
100 && [ "$(head -1 $tmp1)" = 'CREATE USER' ] \
101 || grep -q "^ERROR: .* user name \"$db_user\" already exists$" $tmp2 ; then
102 # Creation OK or user already existing -- no problem here
105 echo "Cannot create PostgreSQL user... This shouldn't have happened."
106 echo "Maybe a problem in your PostgreSQL configuration?"
107 echo "Please report a bug to the Debian bug tracking system"
108 echo "Please include the following output:"
109 echo "createuser's STDOUT:"
111 echo "createuser's STDERR:"
117 if su -s /bin/sh postgres -c "/usr/bin/psql template1" &> /dev/null <<-EOF
118 CREATE USER $db_user WITH PASSWORD '$db_passwd' ;
123 echo "Cannot create PostgreSQL user... This shouldn't have happened."
124 echo "Maybe a problem in your PostgreSQL configuration?"
125 echo "Please report a bug to the Debian bug tracking system"
126 echo "Please include the following output:"
127 echo "CREATE USER's STDOUT:"
129 echo "CREATE USER's STDERR:"
136 # Create the appropriate database
137 tmp1=$(mktemp /tmp/$pattern)
138 tmp2=$(mktemp /tmp/$pattern)
139 if su -s /bin/sh postgres -c "createdb --encoding=UNICODE $db_name" 1> $tmp1 2> $tmp2 \
140 && [ "$(head -1 $tmp1)" = 'CREATE DATABASE' ] \
141 || grep -q "ERROR: .* database \"$db_name\" already exists" $tmp2 ; then
142 # Creation OK or database already existing -- no problem here
146 echo "Cannot create PostgreSQL database... This shouldn't have happened."
147 echo "Maybe a problem in your PostgreSQL configuration?"
148 echo "Please report a bug to the Debian bug tracking system"
149 echo "Please include the following output:"
150 echo "createdb's STDOUT:"
152 echo "createdb's STDERR:"
158 pattern=$(basename $0).XXXXXX
159 tmp1=$(mktemp /tmp/$pattern)
160 tmp2=$(mktemp /tmp/$pattern)
161 if su -s /bin/sh postgres -c "/usr/lib/postgresql/bin/enable_lang plpgsql $db_name" 1> $tmp1 2> $tmp2 \
162 || grep -q "plpgsql added to $db_name" $tmp1 \
163 || grep -q "plpgsql is already enabled in $db_name" $tmp1 ; then
164 # Creation OK or user already existing -- no problem here
168 echo "Cannot enable the PLPGSQL language in the database... This shouldn't have happened."
169 echo "Maybe a problem in your PostgreSQL configuration?"
170 echo "Please report a bug to the Debian bug tracking system"
171 echo "Please include the following output:"
172 echo "enable_lang's STDOUT:"
174 echo "enable_lang's STDERR:"
180 # Install/upgrade the database contents (tables and data)
181 kill -HUP $(head -1 /var/lib/postgres/data/postmaster.pid)
182 su -s /bin/sh gforge -c /usr/lib/gforge/bin/db-upgrade.pl 2>&1 | grep -v ^NOTICE:
184 if [ $p != 0 ] ; then
190 cp -a /etc/postgresql/pg_hba.conf /etc/postgresql/pg_hba.conf.gforge-new
191 if grep -q "### Next line inserted by GForge install" /etc/postgresql/pg_hba.conf.gforge-new
193 perl -pi -e "s/### Next line inserted by GForge install\n//" /etc/postgresql/pg_hba.conf.gforge-new
194 # same problem below with gforge required to be the first host that
195 # uses password, required for allowing change of db_name.
196 perl -pi -e "s/^host.*password\n//" /etc/postgresql/pg_hba.conf.gforge-new
197 perl -pi -e "s/^host.*gforge_passwd\n//" /etc/postgresql/pg_hba.conf.gforge-new
201 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
202 db_user=$(grep ^db_user= /etc/gforge/gforge.conf | cut -d= -f2-)
203 su -s /bin/sh postgres -c "dropdb $db_name" > /dev/null 2>&1 || true
204 su -s /bin/sh postgres -c "dropuser $db_user" > /dev/null 2>&1 || true
205 rm -f /var/lib/postgres/data/gforge_passwd
206 kill -HUP $(head -1 /var/lib/postgres/data/postmaster.pid)
209 if [ -e /etc/sourceforge/local.pl ] ; then
210 db_name=$(perl -e'require "/etc/sourceforge/local.pl"; print "$sys_dbname\n";')
211 elif [ -e /etc/gforge/gforge.conf ] ; then
212 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
216 if [ "x$2" != "x" ] ;then
219 DUMPFILE=/var/lib/gforge/dumps/db_dump
221 if [ "x$3" != "x" ] ;then
226 echo "Dumping $DB database in $DUMPFILE"
227 su -s /bin/sh $DB -c /usr/lib/postgresql/bin/pg_dump $DB > $DUMPFILE
230 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
231 pattern=$(basename $0).XXXXXX
232 newpg=$(mktemp /tmp/$pattern)
233 pg_version=$(dpkg -s postgresql | awk '/^Version: / { print $2 }')
234 if dpkg --compare-versions $pg_version lt 7.3 ; then
235 localtrust="local all trust"
237 localtrust="local all all trust"
239 echo "### Next line inserted by GForge restore" > $newpg
240 echo "$localtrust" >> $newpg
241 #echo "host all 127.0.0.1 255.255.255.255 trust" >> $newpg
242 cat /etc/postgresql/pg_hba.conf >> $newpg
243 mv $newpg /etc/postgresql/pg_hba.conf
244 chmod 644 /etc/postgresql/pg_hba.conf
245 /etc/init.d/postgresql restart
246 if [ "x$2" != "x" ] ;then
249 RESTFILE=/var/lib/gforge/dumps/db_dump
251 echo "Restoring $RESTFILE"
252 su -s /bin/sh postgres -c "dropdb $db_name" || true
253 su -s /bin/sh postgres -c "createdb --encoding=UNICODE $db_name" || true
254 su -s /bin/sh postgres -c "/usr/lib/postgresql/bin/psql -f $RESTFILE $db_name"
255 perl -pi -e "s/### Next line inserted by GForge restore\n//" /etc/postgresql/pg_hba.conf
256 perl -pi -e "s/$localtrust\n//" /etc/postgresql/pg_hba.conf
257 #perl -pi -e "s/host all 127.0.0.1 255.255.255.255 trust\n//" /etc/postgresql/pg_hba.conf
258 /etc/init.d/postgresql reload