aboutsummaryrefslogtreecommitdiff
path: root/etc/pgctl
blob: 4331ca1f3289bfde4fd8dd3e1855a23f22ea7def (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/sh
# file      : etc/pgctl
# copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
# license   : MIT; see accompanying LICENSE file
#
# Designed to simplify controlling brep PostgreSQL server instance.

. `dirname $0`/config

CMD="$1"
SOCKET_DIR="$PG_WORKSPACE_DIR"
OUT_FILE="$PG_WORKSPACE_DIR/out"

# Print usage description and exit.
#
case $CMD in
  init|start|stop|status|connect) ;;
  *)
    echo "Usage: pgctl (init|start|stop|status|connect)"
    exit 1
    ;;
esac

ERROR=0

# Initialization includes creating PostgreSQL DB cluster, creating brep DB
# and schema.
#
if test "$CMD" = "init"; then
  if test -d "$PG_DATA_DIR"; then
    echo "PostgreSQL DB cluster directory $PG_DATA_DIR already exist"
  else
    echo "creating PostgreSQL DB cluster ..."
    pg_ctl initdb -D "$PG_DATA_DIR"
    ERROR=$?

    if test $ERROR -eq 0; then
      echo "cluster created"
    else
      echo "cluster creating failed"
      exit $ERROR;
    fi
  fi
fi

case $CMD in
  start|init)
    # Start DB server if not running yet.
    #
    pg_ctl status -D "$PG_DATA_DIR" 1>/dev/null 2>&1

    if test $? -eq 0; then
      echo "PostgreSQL server already running"
    else
      if test ! -d "$PG_DATA_DIR"; then
        echo "PostgreSQL DB cluster not exist; run '$0 init' first"
	exit 1
      fi

      echo "PostgreSQL server starting ..."

      mkdir -p `dirname $OUT_FILE`
      mkdir -p "$PG_LOG_DIR"
      mkdir -p "$SOCKET_DIR"

      pg_ctl start -D "$PG_DATA_DIR" -w -o \
        "-c port=$PG_PORT -c unix_socket_directories=$SOCKET_DIR \
        -c logging_collector=on -c log_directory=$PG_LOG_DIR" \
	1>"$OUT_FILE" 2>&1

      ERROR=$?

      if test $ERROR -eq 0; then
        echo "server started"
      else
        cat "$OUT_FILE" 1>&2
        echo "server starting failed"
        exit $ERROR
      fi
    fi
    ;;

  status)
    echo "checking PostgreSQL server status ..."
    pg_ctl status -D "$PG_DATA_DIR"
    ;;

  stop)
    pg_ctl status -D "$PG_DATA_DIR" 1>/dev/null 2>&1

    if test $? -eq 0; then
      echo "PostgreSQL server stopping ..."
      pg_ctl stop -D "$PG_DATA_DIR"
      ERROR=$?

      if test $ERROR -eq 0; then
        : # pg_ctl prints "server stopped" on success
      else
        echo "server stopping failed"
        exit $ERROR
      fi
    else
      echo "PostgreSQL server not running"
    fi
    ;;

  connect)
    echo "connecting to PostgreSQL server ..."
    psql --host=$SOCKET_DIR --port=$PG_PORT brep
    ;;
esac

if test "$CMD" = "init"; then
  # Create brep DB if not exist.
  #
  psql --host=$SOCKET_DIR --port=$PG_PORT -c "" brep 1>/dev/null 2>&1

  ERROR=$?

  if test $ERROR -eq 0; then
    echo "brep DB already exist"
  else
    if test $ERROR -eq 2; then
      echo "creating brep DB ..."
      createdb --host=$SOCKET_DIR --port=$PG_PORT brep
      ERROR=$?

      if test $ERROR -eq 0; then
        echo "brep DB created"
      else
        echo "brep DB creating failed"
        exit $ERROR;
      fi
    else
      echo "brep DB existence checking failed"
      exit $ERROR;
    fi
  fi

  # Create brep DB schema if not exist.
  #
  psql --host=$SOCKET_DIR --port=$PG_PORT -c "select count(1) from package" \
       brep 1>/dev/null 2>&1

  ERROR=$?

  if test $ERROR -eq 0; then
    echo "brep DB schema already exist"
  else
    echo "creating brep DB schema ..."

    psql -v ON_ERROR_STOP=1 --host=$SOCKET_DIR --port=$PG_PORT \
	 --file="$PG_SCHEMA_DIR/package.sql" brep 1>"$OUT_FILE" 2>&1

    ERROR=$?

    if test $ERROR -eq 0; then
      echo "brep DB schema created"
    else
      cat "$OUT_FILE" 1>&2
      echo "brep DB schema creating failed"
      exit $ERROR;
    fi
  fi
fi