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 — exiting with error"
32 # this function export in var pg_version the version of postgresql
33 # exit from this script if it do not find the version of postgresql
34 # TODO : try to use function getdist from novaforge
35 function get_pg_version() {
36 # postgresql on debian
37 if [ -f /usr/bin/pg_lsclusters ]
39 pg_version=`/usr/bin/pg_lsclusters | grep 5432 | grep online | cut -d' ' -f1`
42 # postgresql on redhat, fedora, centos
43 if [ -f /var/lib/pgsql/data/PG_VERSION ]
45 pg_version=`cat /var/lib/pgsql/data/PG_VERSION`
48 echo "No database found online on port 5432"
49 echo "Couldn't initialize or upgrade gforge database"
56 if [ $(id -u) != 0 ] ; then
57 echo "You must be root to run this, please enter passwd"
70 # We are with new postgresql working with clusters
71 # This is probably not te most elegant way to deal with database
72 # I install or upgrade on the default cluster if it is online
73 # or I quit gently with a big message
79 echo "Usage: $0 {configure-files|configure|purge|purge-files|dump|restore}"
83 # Tell PostgreSQL to let us use the database
84 export FUSIONFORGE_NO_PLUGINS=true
85 db_passwd=$(forge_get_config database_password)
86 db_name=$(forge_get_config database_name)
87 db_user=$(forge_get_config database_user)
88 pattern=$(basename $0).XXXXXX
90 # PostgreSQL configuration for versions from 7.3 on
91 cp -a ${pg_hba_dir}/pg_hba.conf ${pg_hba_dir}/pg_hba.conf.gforge-new
92 cur=$(mktemp /tmp/$pattern)
93 if ! grep -q 'BEGIN GFORGE BLOCK -- DO NOT EDIT' ${pg_hba_dir}/pg_hba.conf.gforge-new ; then
94 # Make sure our configuration is inside a delimited BLOCK
95 if grep -q "^host.*gforge_passwd$" ${pg_hba_dir}/pg_hba.conf.gforge-new ; then
96 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
97 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
98 elif grep -q "^### Next line inserted by GForge install" ${pg_hba_dir}/pg_hba.conf.gforge-new ; then
99 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
100 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
102 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
103 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
106 echo "### BEGIN GFORGE BLOCK -- DO NOT EDIT" > $cur
107 echo "### END GFORGE BLOCK -- DO NOT EDIT" >> $cur
108 cat ${pg_hba_dir}/pg_hba.conf.gforge-new >> $cur
109 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
112 cur=$(mktemp /tmp/$pattern)
113 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
114 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
119 # Create the appropriate database user
120 export FUSIONFORGE_NO_PLUGINS=true
121 db_passwd=$(forge_get_config database_password)
122 db_name=$(forge_get_config database_name)
123 db_user=$(forge_get_config database_user)
124 pattern=$(basename $0).XXXXXX
125 tmp1=$(mktemp /tmp/$pattern)
126 tmp2=$(mktemp /tmp/$pattern)
127 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<-EOF
128 SET LC_MESSAGES = 'C' ;
129 CREATE USER $db_user WITH PASSWORD '$db_passwd' ;
134 echo "Cannot create PostgreSQL user... This shouldn't have happened."
135 echo "Maybe a problem in your PostgreSQL configuration?"
136 echo "Please report a bug to the Debian bug tracking system"
137 echo "Please include the following output:"
138 echo "CREATE USER's STDOUT:"
140 echo "CREATE USER's STDERR:"
145 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<-EOF
146 SET LC_MESSAGES = 'C' ;
147 CREATE USER ${db_user}_nss WITH PASSWORD '${db_user}_nss' ;
152 echo "Cannot create PostgreSQL user... This shouldn't have happened."
153 echo "Maybe a problem in your PostgreSQL configuration?"
154 echo "Please report a bug to the Debian bug tracking system"
155 echo "Please include the following output:"
156 echo "CREATE USER's STDOUT:"
158 echo "CREATE USER's STDERR:"
163 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<-EOF
164 SET LC_MESSAGES = 'C' ;
165 CREATE USER ${db_user}_mta WITH PASSWORD '${db_user}_mta' ;
170 echo "Cannot create PostgreSQL user... This shouldn't have happened."
171 echo "Maybe a problem in your PostgreSQL configuration?"
172 echo "Please report a bug to the Debian bug tracking system"
173 echo "Please include the following output:"
174 echo "CREATE USER's STDOUT:"
176 echo "CREATE USER's STDERR:"
181 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<-EOF
182 SET LC_MESSAGES = 'C' ;
188 echo "Cannot create PostgreSQL user... This shouldn't have happened."
189 echo "Maybe a problem in your PostgreSQL configuration?"
190 echo "Please report a bug to the Debian bug tracking system"
191 echo "Please include the following output:"
192 echo "CREATE USER's STDOUT:"
194 echo "CREATE USER's STDERR:"
200 # Create the appropriate database
201 if ! exist_db $db_name ; then
202 tmp1=$(mktemp /tmp/$pattern)
203 tmp2=$(mktemp /tmp/$pattern)
204 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<EOF \
205 && [ "$(tail -n +2 $tmp1 | head -1)" = 'CREATE DATABASE' ] ;
206 SET LC_MESSAGES = 'C' ;
207 CREATE DATABASE $db_name WITH TEMPLATE template0 ENCODING 'UNICODE';
215 echo "Cannot create PostgreSQL database... This shouldn't have happened."
216 echo "Maybe a problem in your PostgreSQL configuration?"
217 echo "Please report a bug to the Debian bug tracking system"
218 echo "Please include the following output:"
219 echo "CREATE DATABASE's STDOUT:"
221 echo "CREATE DATABASE's STDERR:"
228 # Grant appropriate permissions to the database (for schema creation)
229 tmp1=$(mktemp /tmp/$pattern)
230 tmp2=$(mktemp /tmp/$pattern)
231 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<EOF \
232 && [ "$(tail -n +2 $tmp1 | head -1)" = 'GRANT' ] ;
233 SET LC_MESSAGES = 'C' ;
234 GRANT CREATE ON DATABASE $db_name TO $db_user ;
241 echo "Cannot grant permissions to PostgreSQL database... This shouldn't have happened."
242 echo "Maybe a problem in your PostgreSQL configuration?"
243 echo "Please report a bug to the Debian bug tracking system"
244 echo "Please include the following output:"
245 echo "GRANT CREATE's STDOUT:"
247 echo "GRANT CREATE's STDERR:"
253 # Enable plpgsql language
254 if [ -f /usr/bin/createlang ]
256 if [ `su -s /bin/sh postgres -c "/usr/bin/createlang -l $db_name | grep plpgsql | wc -l"` != 1 ]
258 su -s /bin/sh postgres -c "/usr/bin/createlang plpgsql $db_name"
260 echo "Procedural language on $db_name already enabled"
263 echo "No way found to enable plpgsql on $db_name here"
266 # Install/upgrade the database contents (tables and data)
267 /usr/share/gforge/bin/old-db-upgrade.pl 2>&1 | grep -v ^NOTICE: | grep -v ^DETAIL: | grep -v ^WARNING: \
268 && p=${PIPESTATUS[0]} \
269 || p=${PIPESTATUS[0]}
270 if [ $p != 0 ] ; then
274 /usr/share/gforge/bin/upgrade-db.php
276 if [ "$need_admin_pw" = "true" ] ; then
281 cp -a ${pg_hba_dir}/pg_hba.conf ${pg_hba_dir}/pg_hba.conf.gforge-new
282 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
285 export FUSIONFORGE_NO_PLUGINS=true
286 db_name=$(forge_get_config database_name)
287 db_user=$(forge_get_config database_user)
288 su -s /bin/sh postgres -c "dropdb $db_name" > /dev/null 2>&1 || true
289 su -s /bin/sh postgres -c "dropuser $db_user" > /dev/null 2>&1 || true
292 # only only for migrating users of the sourceforge package
295 export FUSIONFORGE_NO_PLUGINS=true
296 db_name=$(forge_get_config database_name)
297 if [ "x$2" != "x" ] ;then
300 DUMPFILE=/var/lib/gforge/dumps/db_dump
302 if [ "x$3" != "x" ] ;then
307 echo "Dumping $DB database in $DUMPFILE"
308 su -s /bin/sh $DB -c /usr/lib/postgresql/bin/pg_dump $DB > $DUMPFILE
311 # only only for migrating users of the sourceforge package
314 pg_name=postgresql-$pg_version
315 export FUSIONFORGE_NO_PLUGINS=true
316 db_name=$(forge_get_config database_name)
317 pattern=$(basename $0).XXXXXX
318 newpg=$(mktemp /tmp/$pattern)
319 localtrust="local all all trust"
320 echo "### Next line inserted by GForge restore" > $newpg
321 echo "$localtrust" >> $newpg
322 #echo "host all 127.0.0.1 255.255.255.255 trust" >> $newpg
323 cat ${pg_hba_dir}/pg_hba.conf >> $newpg
324 mv $newpg ${pg_hba_dir}/pg_hba.conf
325 chmod 644 ${pg_hba_dir}/pg_hba.conf
327 # Trying "postgresql" init script...
329 invoke-rc.d postgresql restart || v=$?
330 if test x"$v" = x"100"; then
331 # No "postgresql" init script (for packages << 8.4.4-2)
332 pg_name=postgresql-$pg_version
333 invoke-rc.d ${pg_name} restart
334 elif test x"$v" != x"0"; then
335 # Needed, since we run under "set -e"...
339 if [ "x$2" != "x" ] ;then
342 RESTFILE=/var/lib/gforge/dumps/db_dump
344 echo "Restoring $RESTFILE"
345 su -s /bin/sh postgres -c "dropdb $db_name" || true
346 su -s /bin/sh postgres -c "createdb --encoding=UNICODE $db_name" || true
347 su -s /bin/sh postgres -c "/usr/bin/psql -f $RESTFILE $db_name"
348 perl -pi -e "s/### Next line inserted by GForge restore\n//" ${pg_hba_dir}/pg_hba.conf
349 perl -pi -e "s/$localtrust\n//" ${pg_hba_dir}/pg_hba.conf
351 # Trying "postgresql" init script...
353 invoke-rc.d postgresql reload || v=$?
354 if test x"$v" = x"100"; then
355 # No "postgresql" init script (for packages << 8.4.4-2)
356 pg_name=postgresql-$pg_version
357 invoke-rc.d ${pg_name} reload
358 elif test x"$v" != x"0"; then
359 # Needed, since we run under "set -e"...