5 # Configure postgresql database for GForge
8 # Simple function to know if a db exists
11 su -s /bin/sh postgres -c "psql $1 >/dev/null 2>&1 </dev/null"
16 if [ $(id -u) != 0 ] ; then
17 echo "You must be root to run this, please enter passwd"
29 # Support for new place for pg_hba.conf
30 # I only try to upgrade on the default cluster
31 # I no database is found running, we exit with a big message
32 if [ -x /usr/bin/pg_lsclusters ]
34 # We are with new postgresql working with clusters
35 # This is probably not te most elegant way to deal with database
36 # I install or upgrade on the default cluster if it is online
37 # or I quit gently with a big message
38 pg_version=`/usr/bin/pg_lsclusters | grep 5432 | grep online | cut -d' ' -f1`
39 if [ "x$pg_version" != "x" ]
41 export pg_hba_dir=/etc/postgresql/${pg_version}/main/
43 echo "No database found online on port 5432"
44 echo "Couldn't initialize or upgrade gforge database."
45 echo "Please see postgresql documentation"
46 echo "and run dpkg-reconfigure -plow gforge-db-postgresql"
47 echo "once the problem is solved"
48 echo "exiting without error, but gforge db will not work"
53 export pg_hba_dir=/etc/postgresql
54 if ! pidof postmaster > /dev/null 2> /dev/null ; then
55 echo "No database postmaster found online running"
56 echo "Couldn't initialize or upgrade gforge database."
57 echo "Please see postgresql documentation"
58 echo "and run dpkg-reconfigure -plow gforge-db-postgresql"
59 echo "once the problem is solved"
60 echo "exiting without error, but gforge db will not work"
68 echo "Usage: $0 {configure-files|configure|purge|purge-files|dump|restore}"
72 # Tell PostgreSQL to let us use the database
73 db_passwd=$(grep ^db_password= /etc/gforge/gforge.conf | cut -d= -f2-)
74 ip_address=$(grep ^ip_address= /etc/gforge/gforge.conf | cut -d= -f2-)
75 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
76 db_user=$(grep ^db_user= /etc/gforge/gforge.conf | cut -d= -f2-)
77 db_host=$(grep ^db_host= /etc/gforge/gforge.conf | cut -d= -f2-)
78 pattern=$(basename $0).XXXXXX
79 pg_version=$(dpkg -s postgresql | awk '/^Version: / { print $2 }')
81 if [ "$db_host" == "127.0.0.1" -o "$db_host" == "localhost" ]
83 # Otherwise the line wouldn't be used
84 # And postgres auth would fail
87 if dpkg --compare-versions $pg_version lt 7.3 ; then
88 # PostgreSQL configuration for versions prior to 7.3
89 echo "Configuring for PostgreSQL 7.2"
90 cp -a ${pg_hba_dir}/pg_hba.conf ${pg_hba_dir}/pg_hba.conf.gforge-new
91 # if previous string, else no previous string
92 if grep -q "^host.*gforge_passwd$" ${pg_hba_dir}/pg_hba.conf.gforge-new ; then
93 perl -pi -e "s/^host.*gforge_passwd$/host $db_name $ip_address 255.255.255.255 password gforge_passwd/" ${pg_hba_dir}/pg_hba.conf.gforge-new
95 cur=$(mktemp /tmp/$pattern)
96 echo "### Next line inserted by GForge install" > $cur
97 echo "host $db_name $ip_address 255.255.255.255 password gforge_passwd" >> $cur
98 cat ${pg_hba_dir}/pg_hba.conf.gforge-new >> $cur
99 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
102 su -s /bin/sh postgres -c "touch /var/lib/postgres/data/gforge_passwd"
103 su -s /bin/sh postgres -c "/usr/lib/postgresql/bin/pg_passwd /var/lib/postgres/data/gforge_passwd > /dev/null" <<-EOF
109 # PostgreSQL configuration for versions from 7.3 on
110 echo "Configuring for PostgreSQL > 7.3"
111 cp -a ${pg_hba_dir}/pg_hba.conf ${pg_hba_dir}/pg_hba.conf.gforge-new
112 cur=$(mktemp /tmp/$pattern)
113 if ! grep -q 'BEGIN GFORGE BLOCK -- DO NOT EDIT' ${pg_hba_dir}/pg_hba.conf.gforge-new ; then
114 # Make sure our configuration is inside a delimited BLOCK
115 if grep -q "^host.*gforge_passwd$" ${pg_hba_dir}/pg_hba.conf.gforge-new ; then
116 perl -e "open F, \"${pg_hba_dir}/pg_hba.conf.gforge-new\" or die \$!; undef \$/; \$l=<F>; \$l=~ s/^host.*gforge_passwd\$/### BEGIN GFORGE BLOCK -- DO NOT EDIT\n### END GFORGE BLOCK -- DO NOT EDIT/s; print \$l;" > $cur
117 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
118 elif grep -q "^### Next line inserted by GForge install" ${pg_hba_dir}/pg_hba.conf.gforge-new ; then
119 perl -e "open F, \"${pg_hba_dir}/pg_hba.conf.gforge-new\" or die \$!; undef \$/; \$l=<F>; \$l=~ s/^### Next line inserted by GForge install\nhost $db_name $db_user $ip_address 255.255.255.255 password/### BEGIN GFORGE BLOCK -- DO NOT EDIT\n### END GFORGE BLOCK -- DO NOT EDIT/s; print \$l;" > $cur
120 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
122 perl -e "open F, \"${pg_hba_dir}/pg_hba.conf.gforge-new\" or die \$!; undef \$/; \$l=<F>; \$l=~ s/^host $db_name $db_user.*password\$/### BEGIN GFORGE BLOCK -- DO NOT EDIT\n### END GFORGE BLOCK -- DO NOT EDIT/s; print \$l;" > $cur
123 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
126 echo "### BEGIN GFORGE BLOCK -- DO NOT EDIT" > $cur
127 echo "### END GFORGE BLOCK -- DO NOT EDIT" >> $cur
128 cat ${pg_hba_dir}/pg_hba.conf.gforge-new >> $cur
129 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
132 cur=$(mktemp /tmp/$pattern)
133 perl -e "open F, \"${pg_hba_dir}/pg_hba.conf.gforge-new\" or die \$!; undef \$/; \$l=<F>; \$l=~ s/^### BEGIN GFORGE BLOCK -- DO NOT EDIT.*### END GFORGE BLOCK -- DO NOT EDIT\$/### BEGIN GFORGE BLOCK -- DO NOT EDIT\nhost $db_name $db_user $ip_address 255.255.255.255 password\nhost $db_name gforge_nss $ip_address 255.255.255.255 trust\nhost $db_name gforge_mta $ip_address 255.255.255.255 trust\n### END GFORGE BLOCK -- DO NOT EDIT/ms; print \$l;" > $cur
134 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
137 # Remove old password file, created by 7.2, not used by 7.3
138 if [ -e /var/lib/postgres/data/gforge_passwd ] ; then
139 rm -f /var/lib/postgres/data/gforge_passwd
145 # Create the appropriate database user
146 pg_version=$(dpkg -s postgresql | awk '/^Version: / { print $2 }')
147 db_passwd=$(grep ^db_password= /etc/gforge/gforge.conf | cut -d= -f2-)
148 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
149 db_user=$(grep ^db_user= /etc/gforge/gforge.conf | cut -d= -f2-)
150 pattern=$(basename $0).XXXXXX
151 tmp1=$(mktemp /tmp/$pattern)
152 tmp2=$(mktemp /tmp/$pattern)
153 if dpkg --compare-versions $pg_version lt 7.3 ; then
154 if su -s /bin/sh postgres -c "createuser --no-createdb --no-adduser $db_user" 1> $tmp1 2> $tmp2 \
155 && [ "$(head -1 $tmp1)" = 'CREATE USER' ] \
156 || grep -q "^ERROR: .* user name \"$db_user\" already exists$" $tmp2 ; then
157 # Creation OK or user already existing -- no problem here
160 echo "Cannot create PostgreSQL user... This shouldn't have happened."
161 echo "Maybe a problem in your PostgreSQL configuration?"
162 echo "Please report a bug to the Debian bug tracking system"
163 echo "Please include the following output:"
164 echo "createuser's STDOUT:"
166 echo "createuser's STDERR:"
172 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<-EOF
173 CREATE USER $db_user WITH PASSWORD '$db_passwd' ;
178 echo "Cannot create PostgreSQL user... This shouldn't have happened."
179 echo "Maybe a problem in your PostgreSQL configuration?"
180 echo "Please report a bug to the Debian bug tracking system"
181 echo "Please include the following output:"
182 echo "CREATE USER's STDOUT:"
184 echo "CREATE USER's STDERR:"
189 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<-EOF
190 CREATE USER gforge_nss WITH PASSWORD '' ;
195 echo "Cannot create PostgreSQL user... This shouldn't have happened."
196 echo "Maybe a problem in your PostgreSQL configuration?"
197 echo "Please report a bug to the Debian bug tracking system"
198 echo "Please include the following output:"
199 echo "CREATE USER's STDOUT:"
201 echo "CREATE USER's STDERR:"
206 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<-EOF
207 CREATE USER gforge_mta WITH PASSWORD '' ;
212 echo "Cannot create PostgreSQL user... This shouldn't have happened."
213 echo "Maybe a problem in your PostgreSQL configuration?"
214 echo "Please report a bug to the Debian bug tracking system"
215 echo "Please include the following output:"
216 echo "CREATE USER's STDOUT:"
218 echo "CREATE USER's STDERR:"
225 # Create the appropriate database
226 tmp1=$(mktemp /tmp/$pattern)
227 tmp2=$(mktemp /tmp/$pattern)
228 if ! exist_db $db_name ; then
229 if su -s /bin/sh postgres -c "createdb --encoding=UNICODE $db_name" 1> $tmp1 2> $tmp2 \
230 && [ "$(head -1 $tmp1)" = 'CREATE DATABASE' ] \
236 echo "Cannot create PostgreSQL database... This shouldn't have happened."
237 echo "Maybe a problem in your PostgreSQL configuration?"
238 echo "Please report a bug to the Debian bug tracking system"
239 echo "Please include the following output:"
240 echo "createdb's STDOUT:"
242 echo "createdb's STDERR:"
249 # Enable plpgsql language
251 pattern=$(basename $0).XXXXXX
252 tmp1=$(mktemp /tmp/$pattern)
253 tmp2=$(mktemp /tmp/$pattern)
254 if [ -f /usr/lib/postgresql/bin/enable_lang ]
256 if su -s /bin/sh postgres -c "/usr/lib/postgresql/bin/enable_lang plpgsql $db_name" 1> $tmp1 2> $tmp2 \
257 || grep -q "plpgsql added to $db_name" $tmp1 \
258 || grep -q "plpgsql is already enabled in $db_name" $tmp1 ; then
259 # Creation OK or user already existing -- no problem here
263 echo "Cannot enable the PLPGSQL language in the database... This shouldn't have happened."
264 echo "Maybe a problem in your PostgreSQL configuration?"
265 echo "Please report a bug to the Debian bug tracking system"
266 echo "Please include the following output:"
267 echo "enable_lang's STDOUT:"
269 echo "enable_lang's STDERR:"
276 if [ -f /usr/bin/createlang ]
278 if [ `su -s /bin/sh postgres -c "/usr/bin/createlang -l $db_name | grep plpgsql | wc -l"` != 1 ]
280 su -s /bin/sh postgres -c "/usr/bin/createlang plpgsql $db_name"
282 echo "Procedural language on $db_name already enabled"
285 echo "No way found to enable plpgsql on $db_name here"
288 # Install/upgrade the database contents (tables and data)
289 su -s /bin/sh gforge -c /usr/lib/gforge/bin/db-upgrade.pl 2>&1 | grep -v ^NOTICE:
291 if [ $p != 0 ] ; then
294 # Must be root to reorg these files, but only have to do it once
295 [ ! -f /var/lib/gforge/db/20050127-frs-reorg.done ] &&\
296 (/usr/lib/gforge/db/20050127-frs-reorg.php \
297 -d include_path=/etc/gforge:/usr/share/gforge/:/usr/share/gforge/www/include &&\
298 touch /var/lib/gforge/db/20050127-frs-reorg.done) || true
299 # Le last line had the bad idea to create a cache file owned by root
300 rm -f /var/cache/gforge/English.cache
304 cp -a ${pg_hba_dir}/pg_hba.conf ${pg_hba_dir}/pg_hba.conf.gforge-new
305 if grep -q "### Next line inserted by GForge install" ${pg_hba_dir}/pg_hba.conf.gforge-new
307 perl -pi -e "s/### Next line inserted by GForge install\n//" ${pg_hba_dir}/pg_hba.conf.gforge-new
308 # same problem below with gforge required to be the first host that
309 # uses password, required for allowing change of db_name.
310 perl -pi -e "s/^host.*password\n//" ${pg_hba_dir}/pg_hba.conf.gforge-new
311 perl -pi -e "s/^host.*gforge_passwd\n//" ${pg_hba_dir}/pg_hba.conf.gforge-new
315 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
316 db_user=$(grep ^db_user= /etc/gforge/gforge.conf | cut -d= -f2-)
317 su -s /bin/sh postgres -c "dropdb $db_name" > /dev/null 2>&1 || true
318 su -s /bin/sh postgres -c "dropuser $db_user" > /dev/null 2>&1 || true
319 rm -f /var/lib/postgres/data/gforge_passwd
320 [ -f /var/lib/postgres/data/postmaster.pid ] && kill -HUP $(head -1 /var/lib/postgres/data/postmaster.pid) || true
323 if [ -e /etc/sourceforge/local.pl ] ; then
324 db_name=$(perl -e'require "/etc/sourceforge/local.pl"; print "$sys_dbname\n";')
325 elif [ -e /etc/gforge/gforge.conf ] ; then
326 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
330 if [ "x$2" != "x" ] ;then
333 DUMPFILE=/var/lib/gforge/dumps/db_dump
335 if [ "x$3" != "x" ] ;then
340 echo "Dumping $DB database in $DUMPFILE"
341 su -s /bin/sh $DB -c /usr/lib/postgresql/bin/pg_dump $DB > $DUMPFILE
344 if [ "x$pg_version" != "x" ]
346 pg_name=postgresql-$pg_version
350 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
351 pattern=$(basename $0).XXXXXX
352 newpg=$(mktemp /tmp/$pattern)
353 pg_version=$(dpkg -s postgresql | awk '/^Version: / { print $2 }')
354 if dpkg --compare-versions $pg_version lt 7.3 ; then
355 localtrust="local all trust"
357 localtrust="local all all trust"
359 echo "### Next line inserted by GForge restore" > $newpg
360 echo "$localtrust" >> $newpg
361 #echo "host all 127.0.0.1 255.255.255.255 trust" >> $newpg
362 cat ${pg_hba_dir}/pg_hba.conf >> $newpg
363 mv $newpg ${pg_hba_dir}/pg_hba.conf
364 chmod 644 ${pg_hba_dir}/pg_hba.conf
365 invoke-rc.d ${pg_name} restart
366 if [ "x$2" != "x" ] ;then
369 RESTFILE=/var/lib/gforge/dumps/db_dump
371 echo "Restoring $RESTFILE"
372 su -s /bin/sh postgres -c "dropdb $db_name" || true
373 su -s /bin/sh postgres -c "createdb --encoding=UNICODE $db_name" || true
374 su -s /bin/sh postgres -c "/usr/lib/postgresql/bin/psql -f $RESTFILE $db_name"
375 perl -pi -e "s/### Next line inserted by GForge restore\n//" ${pg_hba_dir}/pg_hba.conf
376 perl -pi -e "s/$localtrust\n//" ${pg_hba_dir}/pg_hba.conf
377 invoke-rc.d ${pg_name} reload