-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgtest.sh
executable file
·133 lines (126 loc) · 4.21 KB
/
pgtest.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#! /bin/bash
#
# PostRR test setup helper script
#
# Copyright (C) 2012 Sebastian 'tokkee' Harl <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
if test -z "$PG_CONFIG"; then
PG_CONFIG=`which pg_config`
fi
if test -z "$PG_CONFIG"; then
echo "pg_config not found!" >&2
exit 1
fi
BIN_DIR=`$PG_CONFIG --bindir`
if test -z "$TARGET"; then
TARGET=`pwd`/target
fi
PWD_esc="$( echo "$PWD" | sed -e 's/\//\\\//g' )"
TARGET_esc="$( echo "$TARGET" | sed -e 's/\//\\\//g' )"
set -e
case "$1" in
setup)
if test $# -ne 1; then
echo "Too many arguments!" >&2
echo "Usage: $0 setup" >&2
exit 1
fi
mkdir -p $TARGET/var/lib/postgresql/main
mkdir -p $TARGET/var/run/postgresql
mkdir -p $TARGET/var/log/postgresql/main
$BIN_DIR/initdb -D $TARGET/var/lib/postgresql/main
sed -r -i -e 's/^#port = 5432/port = 2345/' \
$TARGET/var/lib/postgresql/main/postgresql.conf
sed -r -i -e "s/^#dynamic_library_path = '\\\$libdir'/dynamic_library_path = '\$libdir:$PWD_esc\/src'/" $TARGET/var/lib/postgresql/main/postgresql.conf
sed -r -i -e "s/^#unix_socket_directory = ''/unix_socket_directory = '$TARGET_esc\/var\/run\/postgresql'/" $TARGET/var/lib/postgresql/main/postgresql.conf
$0 start -B
$BIN_DIR/createdb -e -h $TARGET/var/run/postgresql/ -p 2345 "$( id -un )"
$0 stop
;;
client)
libedit=$( ldd $BIN_DIR/psql 2> /dev/null \
| grep -Eo '=> [^ ]+\/libedit\.so\..? ' | cut -d' ' -f2 )
if test -n "$libedit"; then
libdir=${libedit/libedit.*/}
libreadline=$( ls $libdir/libreadline.so* 2> /dev/null | head -n1 )
if test -n "$libreadline"; then
export LD_PRELOAD="$LD_PRELOAD:$libreadline"
fi
fi
shift
$BIN_DIR/psql -h $TARGET/var/run/postgresql/ -p 2345 "$@"
;;
stop)
if test $# -ne 1; then
echo "Too many arguments!" >&2
echo "Usage: $0 stop" >&2
exit 1
fi
$BIN_DIR/pg_ctl -D $TARGET/var/lib/postgresql/main stop
;;
start)
shift
if test "$1" = "-B"; then
shift
$BIN_DIR/pg_ctl -D $TARGET/var/lib/postgresql/main -l logfile -w start "$@"
else
$BIN_DIR/postgres -D $TARGET/var/lib/postgresql/main "$@"
fi
;;
restart)
$0 stop && $0 start -B
;;
dump)
shift
$BIN_DIR/pg_dump -h $TARGET/var/run/postgresql/ -p 2345 "$@"
;;
restore)
shift
$BIN_DIR/pg_restore -h $TARGET/var/run/postgresql/ -p 2345 "$@"
;;
*)
echo "Usage: $0 setup|client|stop|start" >&2
echo ""
echo " - setup"
echo " Set up a new PostgreSQL server listening on port 2345."
echo " - client [<psql args>]"
echo " Start a PostgreSQL interactive terminal connected to the"
echo " PostgreSQL server on port 2345."
echo " - start [-B [<postgres args>]]"
echo " Start the PostgreSQL server."
echo " - stop"
echo " Stop the PostgreSQL server."
echo " - restart"
echo " Restart a background PostgreSQL server process."
echo ""
echo "Environment variables:"
echo " - TARGET"
echo " Target directory of the PostgreSQL test setup."
if test "$1" = "help"; then
exit 0
fi
exit 1
;;
esac
# vim: set tw=0 :