3 # Configure postgresql database for GForge
6 # Simple function to know if a db exists
9 su -s /bin/sh postgres -c "psql $1 >/dev/null 2>&1 </dev/null"
12 # this function export in var pg_hba_dir the directory of postgresql
13 # exit from this script if it do not find the directory of postgresql
14 # TODO : try to use function getdist from novaforge
15 function get_pg_hba_dir() {
16 # postgresql on debian
17 if [ -d /etc/postgresql/${pg_version}/main/ ]
19 export pg_hba_dir=/etc/postgresql/${pg_version}/main/
22 # postgresql on redhat, fedora, centos
23 if [ -d /var/lib/pgsql/data/ ]
25 export pg_hba_dir=/var/lib/pgsql/data/
28 echo "No pg_hba_dir found"
29 echo "exiting without error, but gforge db will not work"
33 # this function export in var pg_version the version of postgresql
34 # exit from this script if it do not find the version of postgresql
35 # TODO : try to use function getdist from novaforge
36 function get_pg_version() {
37 # postgresql on debian
38 if [ -f /usr/bin/pg_lsclusters ]
40 pg_version=`/usr/bin/pg_lsclusters | grep 5432 | grep online | cut -d' ' -f1`
43 # postgresql on redhat, fedora, centos
44 if [ -f /var/lib/pgsql/data/PG_VERSION ]
46 pg_version=`cat /var/lib/pgsql/data/PG_VERSION`
49 echo "No database found online on port 5432"
50 echo "Couldn't initialize or upgrade gforge database."
51 echo "Please see postgresql documentation"
52 echo "and run dpkg-reconfigure -plow gforge-db-postgresql"
53 echo "once the problem is solved"
54 echo "exiting without error, but gforge db will not work"
62 if [ $(id -u) != 0 ] ; then
63 echo "You must be root to run this, please enter passwd"
76 # We are with new postgresql working with clusters
77 # This is probably not te most elegant way to deal with database
78 # I install or upgrade on the default cluster if it is online
79 # or I quit gently with a big message
85 echo "Usage: $0 {configure-files|configure|purge|purge-files|dump|restore}"
89 # Tell PostgreSQL to let us use the database
90 export FUSIONFORGE_NO_PLUGINS=true
91 db_passwd=$(/usr/share/gforge/bin/forge_get_config database_password)
92 db_name=$(/usr/share/gforge/bin/forge_get_config database_name)
93 db_user=$(/usr/share/gforge/bin/forge_get_config database_user)
94 pattern=$(basename $0).XXXXXX
96 # PostgreSQL configuration for versions from 7.3 on
97 cp -a ${pg_hba_dir}/pg_hba.conf ${pg_hba_dir}/pg_hba.conf.gforge-new
98 cur=$(mktemp /tmp/$pattern)
99 if ! grep -q 'BEGIN GFORGE BLOCK -- DO NOT EDIT' ${pg_hba_dir}/pg_hba.conf.gforge-new ; then
100 # Make sure our configuration is inside a delimited BLOCK
101 if grep -q "^host.*gforge_passwd$" ${pg_hba_dir}/pg_hba.conf.gforge-new ; then
102 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
103 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
104 elif grep -q "^### Next line inserted by GForge install" ${pg_hba_dir}/pg_hba.conf.gforge-new ; then
105 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 [0-9. ]+ password/### BEGIN GFORGE BLOCK -- DO NOT EDIT\n### END GFORGE BLOCK -- DO NOT EDIT/s; print \$l;" > $cur
106 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
108 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
109 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
112 echo "### BEGIN GFORGE BLOCK -- DO NOT EDIT" > $cur
113 echo "### END GFORGE BLOCK -- DO NOT EDIT" >> $cur
114 cat ${pg_hba_dir}/pg_hba.conf.gforge-new >> $cur
115 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
118 cur=$(mktemp /tmp/$pattern)
119 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\n# user which is used by libnss to access the DB (see \/etc\/nss-pgsql.conf)\nlocal $db_name ${db_user}_nss trust\nlocal $db_name list ident\nlocal $db_name ${db_user}_mta md5\n### END GFORGE BLOCK -- DO NOT EDIT/ms; print \$l;" > $cur
120 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
125 # Create the appropriate database user
126 export FUSIONFORGE_NO_PLUGINS=true
127 db_passwd=$(/usr/share/gforge/bin/forge_get_config database_password)
128 db_name=$(/usr/share/gforge/bin/forge_get_config database_name)
129 db_user=$(/usr/share/gforge/bin/forge_get_config database_user)
130 pattern=$(basename $0).XXXXXX
131 tmp1=$(mktemp /tmp/$pattern)
132 tmp2=$(mktemp /tmp/$pattern)
133 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<-EOF
134 SET LC_MESSAGES = 'C' ;
135 CREATE USER $db_user WITH PASSWORD '$db_passwd' ;
140 echo "Cannot create PostgreSQL user... This shouldn't have happened."
141 echo "Maybe a problem in your PostgreSQL configuration?"
142 echo "Please report a bug to the Debian bug tracking system"
143 echo "Please include the following output:"
144 echo "CREATE USER's STDOUT:"
146 echo "CREATE USER's STDERR:"
151 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<-EOF
152 SET LC_MESSAGES = 'C' ;
153 CREATE USER ${db_user}_nss WITH PASSWORD '${db_user}_nss' ;
158 echo "Cannot create PostgreSQL user... This shouldn't have happened."
159 echo "Maybe a problem in your PostgreSQL configuration?"
160 echo "Please report a bug to the Debian bug tracking system"
161 echo "Please include the following output:"
162 echo "CREATE USER's STDOUT:"
164 echo "CREATE USER's STDERR:"
169 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<-EOF
170 SET LC_MESSAGES = 'C' ;
171 CREATE USER ${db_user}_mta WITH PASSWORD '${db_user}_mta' ;
176 echo "Cannot create PostgreSQL user... This shouldn't have happened."
177 echo "Maybe a problem in your PostgreSQL configuration?"
178 echo "Please report a bug to the Debian bug tracking system"
179 echo "Please include the following output:"
180 echo "CREATE USER's STDOUT:"
182 echo "CREATE USER's STDERR:"
187 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<-EOF
188 SET LC_MESSAGES = 'C' ;
194 echo "Cannot create PostgreSQL user... This shouldn't have happened."
195 echo "Maybe a problem in your PostgreSQL configuration?"
196 echo "Please report a bug to the Debian bug tracking system"
197 echo "Please include the following output:"
198 echo "CREATE USER's STDOUT:"
200 echo "CREATE USER's STDERR:"
206 # Create the appropriate database
207 tmp1=$(mktemp /tmp/$pattern)
208 tmp2=$(mktemp /tmp/$pattern)
209 if ! exist_db $db_name ; then
210 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<EOF \
211 && [ "$(tail -n +2 $tmp1 | head -1)" = 'CREATE DATABASE' ] ;
212 SET LC_MESSAGES = 'C' ;
213 CREATE DATABASE $db_name WITH TEMPLATE template0 ENCODING 'UNICODE';
221 echo "Cannot create PostgreSQL database... This shouldn't have happened."
222 echo "Maybe a problem in your PostgreSQL configuration?"
223 echo "Please report a bug to the Debian bug tracking system"
224 echo "Please include the following output:"
225 echo "CREATE DATABASE's STDOUT:"
227 echo "CREATE DATABASE's STDERR:"
234 # Grant appropriate permissions to the database (for schema creation)
235 tmp1=$(mktemp /tmp/$pattern)
236 tmp2=$(mktemp /tmp/$pattern)
237 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<EOF \
238 && [ "$(tail -n +2 $tmp1 | head -1)" = 'GRANT' ] ;
239 SET LC_MESSAGES = 'C' ;
240 GRANT CREATE ON DATABASE $db_name TO $db_user ;
247 echo "Cannot grant permissions to PostgreSQL database... This shouldn't have happened."
248 echo "Maybe a problem in your PostgreSQL configuration?"
249 echo "Please report a bug to the Debian bug tracking system"
250 echo "Please include the following output:"
251 echo "GRANT CREATE's STDOUT:"
253 echo "GRANT CREATE's STDERR:"
259 # Enable plpgsql language
260 if [ -f /usr/bin/createlang ]
262 if [ `su -s /bin/sh postgres -c "/usr/bin/createlang -l $db_name | grep plpgsql | wc -l"` != 1 ]
264 su -s /bin/sh postgres -c "/usr/bin/createlang plpgsql $db_name"
266 echo "Procedural language on $db_name already enabled"
269 echo "No way found to enable plpgsql on $db_name here"
272 # Install/upgrade the database contents (tables and data)
273 /usr/share/gforge/bin/old-db-upgrade.pl 2>&1 | grep -v ^NOTICE: | grep -v ^DETAIL: | grep -v ^WARNING: \
274 && p=${PIPESTATUS[0]} \
275 || p=${PIPESTATUS[0]}
276 if [ $p != 0 ] ; then
280 /usr/share/gforge/bin/upgrade-db.php
282 if [ "$need_admin_pw" = "true" ] ; then
287 cp -a ${pg_hba_dir}/pg_hba.conf ${pg_hba_dir}/pg_hba.conf.gforge-new
288 perl -pi -e "BEGIN { undef \$/; } s/^### BEGIN GFORGE BLOCK -- DO NOT EDIT.*### END GFORGE BLOCK -- DO NOT EDIT\n//ms;" ${pg_hba_dir}/pg_hba.conf.gforge-new
291 export FUSIONFORGE_NO_PLUGINS=true
292 db_name=$(/usr/share/gforge/bin/forge_get_config database_name)
293 db_user=$(/usr/share/gforge/bin/forge_get_config database_user)
294 su -s /bin/sh postgres -c "dropdb $db_name" > /dev/null 2>&1 || true
295 su -s /bin/sh postgres -c "dropuser $db_user" > /dev/null 2>&1 || true
298 # only only for migrating users of the sourceforge package
301 export FUSIONFORGE_NO_PLUGINS=true
302 db_name=$(/usr/share/gforge/bin/forge_get_config database_name)
303 if [ "x$2" != "x" ] ;then
306 DUMPFILE=/var/lib/gforge/dumps/db_dump
308 if [ "x$3" != "x" ] ;then
313 echo "Dumping $DB database in $DUMPFILE"
314 su -s /bin/sh $DB -c /usr/lib/postgresql/bin/pg_dump $DB > $DUMPFILE
317 # only only for migrating users of the sourceforge package
320 pg_name=postgresql-$pg_version
321 export FUSIONFORGE_NO_PLUGINS=true
322 db_name=$(/usr/share/gforge/bin/forge_get_config database_name)
323 pattern=$(basename $0).XXXXXX
324 newpg=$(mktemp /tmp/$pattern)
325 localtrust="local all all trust"
326 echo "### Next line inserted by GForge restore" > $newpg
327 echo "$localtrust" >> $newpg
328 #echo "host all 127.0.0.1 255.255.255.255 trust" >> $newpg
329 cat ${pg_hba_dir}/pg_hba.conf >> $newpg
330 mv $newpg ${pg_hba_dir}/pg_hba.conf
331 chmod 644 ${pg_hba_dir}/pg_hba.conf
333 # Trying "postgresql" init script...
335 invoke-rc.d postgresql restart || v=$?
336 if test x"$v" = x"100"; then
337 # No "postgresql" init script (for packages << 8.4.4-2)
338 pg_name=postgresql-$pg_version
339 invoke-rc.d ${pg_name} restart
340 elif test x"$v" != x"0"; then
341 # Needed, since we run under "set -e"...
345 if [ "x$2" != "x" ] ;then
348 RESTFILE=/var/lib/gforge/dumps/db_dump
350 echo "Restoring $RESTFILE"
351 su -s /bin/sh postgres -c "dropdb $db_name" || true
352 su -s /bin/sh postgres -c "createdb --encoding=UNICODE $db_name" || true
353 su -s /bin/sh postgres -c "/usr/bin/psql -f $RESTFILE $db_name"
354 perl -pi -e "s/### Next line inserted by GForge restore\n//" ${pg_hba_dir}/pg_hba.conf
355 perl -pi -e "s/$localtrust\n//" ${pg_hba_dir}/pg_hba.conf
357 # Trying "postgresql" init script...
359 invoke-rc.d postgresql reload || v=$?
360 if test x"$v" = x"100"; then
361 # No "postgresql" init script (for packages << 8.4.4-2)
362 pg_name=postgresql-$pg_version
363 invoke-rc.d ${pg_name} reload
364 elif test x"$v" != x"0"; then
365 # Needed, since we run under "set -e"...