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 pattern=$(basename $0).XXXXXX
34 pg_version=$(dpkg -s postgresql | awk '/^Version: / { print $2 }')
35 if dpkg --compare-versions $pg_version lt 7.3 ; then
36 # PostgreSQL configuration for versions prior to 7.3
37 echo "Configuring for PostgreSQL 7.2"
38 cp -a /etc/postgresql/pg_hba.conf /etc/postgresql/pg_hba.conf.gforge-new
39 # if previous string, else no previous string
40 if grep -q "^host.*gforge_passwd$" /etc/postgresql/pg_hba.conf.gforge-new ; then
41 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
43 cur=$(mktemp /tmp/$pattern)
44 echo "### Next line inserted by GForge install" > $cur
45 echo "host $db_name $ip_address 255.255.255.255 password gforge_passwd" >> $cur
46 cat /etc/postgresql/pg_hba.conf.gforge-new >> $cur
47 cat $cur > /etc/postgresql/pg_hba.conf.gforge-new
50 su -s /bin/sh postgres -c "touch /var/lib/postgres/data/gforge_passwd"
51 su -s /bin/sh postgres -c "/usr/lib/postgresql/bin/pg_passwd /var/lib/postgres/data/gforge_passwd > /dev/null" <<-EOF
57 # PostgreSQL configuration for versions from 7.3 on
58 echo "Configuring for PostgreSQL 7.3"
59 cp -a /etc/postgresql/pg_hba.conf /etc/postgresql/pg_hba.conf.gforge-new
60 # if 7.2 formatted string, elseif 7.3 string, else no previous string
61 if grep -q "^host.*gforge_passwd$" /etc/postgresql/pg_hba.conf.gforge-new ; then
62 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
63 # the below could cause issues if gforge's line isnt at the top of the file, but its
64 # the only way to allow changing of the db_name.
65 elif grep -q "^host.*password$" /etc/postgresql/pg_hba.conf.gforge-new ; then
66 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
68 cur=$(mktemp /tmp/$pattern)
69 echo "### Next line inserted by GForge install" > $cur
70 echo "host $db_name $db_user $ip_address 255.255.255.255 password" >> $cur
71 cat /etc/postgresql/pg_hba.conf.gforge-new >> $cur
72 cat $cur > /etc/postgresql/pg_hba.conf.gforge-new
75 # Remove old password file, created by 7.2, not used by 7.3
76 if [ -e /var/lib/postgres/data/gforge_passwd ] ; then
77 rm -f /var/lib/postgres/data/gforge_passwd
83 # Create the appropriate database user
84 pg_version=$(dpkg -s postgresql | awk '/^Version: / { print $2 }')
85 db_passwd=$(grep ^db_password= /etc/gforge/gforge.conf | cut -d= -f2-)
86 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
87 db_user=$(grep ^db_user= /etc/gforge/gforge.conf | cut -d= -f2-)
88 pattern=$(basename $0).XXXXXX
89 tmp1=$(mktemp /tmp/$pattern)
90 tmp2=$(mktemp /tmp/$pattern)
91 if dpkg --compare-versions $pg_version lt 7.3 ; then
92 if su -s /bin/sh postgres -c "createuser --no-createdb --no-adduser $db_user" 1> $tmp1 2> $tmp2 \
93 && [ "$(head -1 $tmp1)" = 'CREATE USER' ] \
94 || grep -q "^ERROR: .* user name \"$db_user\" already exists$" $tmp2 ; then
95 # Creation OK or user already existing -- no problem here
98 echo "Cannot create PostgreSQL user... This shouldn't have happened."
99 echo "Maybe a problem in your PostgreSQL configuration?"
100 echo "Please report a bug to the Debian bug tracking system"
101 echo "Please include the following output:"
102 echo "createuser's STDOUT:"
104 echo "createuser's STDERR:"
110 if su -s /bin/sh postgres -c "/usr/bin/psql template1" &> /dev/null <<-EOF
111 CREATE USER $db_user WITH PASSWORD '$db_passwd' ;
116 echo "Cannot create PostgreSQL user... This shouldn't have happened."
117 echo "Maybe a problem in your PostgreSQL configuration?"
118 echo "Please report a bug to the Debian bug tracking system"
119 echo "Please include the following output:"
120 echo "CREATE USER's STDOUT:"
122 echo "CREATE USER's STDERR:"
129 # Create the appropriate database
130 tmp1=$(mktemp /tmp/$pattern)
131 tmp2=$(mktemp /tmp/$pattern)
132 if su -s /bin/sh postgres -c "createdb --encoding=UNICODE $db_name" 1> $tmp1 2> $tmp2 \
133 && [ "$(head -1 $tmp1)" = 'CREATE DATABASE' ] \
134 || grep -q "ERROR: .* database \"$db_name\" already exists" $tmp2 ; then
135 # Creation OK or database already existing -- no problem here
139 echo "Cannot create PostgreSQL database... This shouldn't have happened."
140 echo "Maybe a problem in your PostgreSQL configuration?"
141 echo "Please report a bug to the Debian bug tracking system"
142 echo "Please include the following output:"
143 echo "createdb's STDOUT:"
145 echo "createdb's STDERR:"
151 pattern=$(basename $0).XXXXXX
152 tmp1=$(mktemp /tmp/$pattern)
153 tmp2=$(mktemp /tmp/$pattern)
154 if su -s /bin/sh postgres -c "/usr/lib/postgresql/bin/enable_lang plpgsql $db_name" 1> $tmp1 2> $tmp2 \
155 || grep -q "plpgsql added to $db_name" $tmp1 \
156 || grep -q "plpgsql is already enabled in $db_name" $tmp1 ; then
157 # Creation OK or user already existing -- no problem here
161 echo "Cannot enable the PLPGSQL language in the database... This shouldn't have happened."
162 echo "Maybe a problem in your PostgreSQL configuration?"
163 echo "Please report a bug to the Debian bug tracking system"
164 echo "Please include the following output:"
165 echo "enable_lang's STDOUT:"
167 echo "enable_lang's STDERR:"
173 # Install/upgrade the database contents (tables and data)
174 kill -HUP $(head -1 /var/lib/postgres/data/postmaster.pid)
175 /usr/lib/gforge/bin/db-upgrade.pl 2>&1 | grep -v ^NOTICE:
177 if [ $p != 0 ] ; then
183 cp -a /etc/postgresql/pg_hba.conf /etc/postgresql/pg_hba.conf.gforge-new
184 if grep -q "### Next line inserted by GForge install" /etc/postgresql/pg_hba.conf.gforge-new
186 perl -pi -e "s/### Next line inserted by GForge install\n//" /etc/postgresql/pg_hba.conf.gforge-new
187 # same problem below with gforge required to be the first host that
188 # uses password, required for allowing change of db_name.
189 perl -pi -e "s/^host.*password\n//" /etc/postgresql/pg_hba.conf.gforge-new
190 perl -pi -e "s/^host.*gforge_passwd\n//" /etc/postgresql/pg_hba.conf.gforge-new
194 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
195 db_user=$(grep ^db_user= /etc/gforge/gforge.conf | cut -d= -f2-)
196 su -s /bin/sh postgres -c "dropdb $db_name" > /dev/null 2>&1 || true
197 su -s /bin/sh postgres -c "dropuser $db_user" > /dev/null 2>&1 || true
198 rm -f /var/lib/postgres/data/gforge_passwd
199 kill -HUP $(head -1 /var/lib/postgres/data/postmaster.pid)
202 if [ -e /etc/sourceforge/local.pl ] ; then
203 db_name=$(perl -e'require "/etc/sourceforge/local.pl"; print "$sys_dbname\n";')
204 elif [ -e /etc/gforge/gforge.conf ] ; then
205 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
209 if [ "x$2" != "x" ] ;then
212 DUMPFILE=/var/lib/gforge/dumps/db_dump
214 if [ "x$3" != "x" ] ;then
219 echo "Dumping $DB database in $DUMPFILE"
220 su -s /bin/sh $DB -c /usr/lib/postgresql/bin/pg_dump $DB > $DUMPFILE
223 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
224 pattern=$(basename $0).XXXXXX
225 newpg=$(mktemp /tmp/$pattern)
226 pg_version=$(dpkg -s postgresql | awk '/^Version: / { print $2 }')
227 if dpkg --compare-versions $pg_version lt 7.3 ; then
228 localtrust="local all trust"
230 localtrust="local all all trust"
232 echo "### Next line inserted by GForge restore" > $newpg
233 echo "$localtrust" >> $newpg
234 #echo "host all 127.0.0.1 255.255.255.255 trust" >> $newpg
235 cat /etc/postgresql/pg_hba.conf >> $newpg
236 mv $newpg /etc/postgresql/pg_hba.conf
237 chmod 644 /etc/postgresql/pg_hba.conf
238 /etc/init.d/postgresql restart
239 if [ "x$2" != "x" ] ;then
242 RESTFILE=/var/lib/gforge/dumps/db_dump
244 echo "Restoring $RESTFILE"
245 su -s /bin/sh postgres -c "dropdb $db_name" || true
246 su -s /bin/sh postgres -c "createdb --encoding=UNICODE $db_name" || true
247 su -s /bin/sh postgres -c "/usr/lib/postgresql/bin/psql -f $RESTFILE $db_name"
248 perl -pi -e "s/### Next line inserted by GForge restore\n//" /etc/postgresql/pg_hba.conf
249 perl -pi -e "s/$localtrust\n//" /etc/postgresql/pg_hba.conf
250 #perl -pi -e "s/host all 127.0.0.1 255.255.255.255 trust\n//" /etc/postgresql/pg_hba.conf
251 /etc/init.d/postgresql reload