aboutsummaryrefslogtreecommitdiff
path: root/etc/pgctl
blob: 26ad888373221c36d098401dd5d62e828e0a1b4d (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
#!/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"

# 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

# Initilization includes creating PostgreSQL DB cluster, creating and
# populating brep DB.
#
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 $PG_OUT_FILE`
      mkdir -p "$PG_LOG_DIR"
      mkdir -p "$PG_SOCKET_DIR"

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

      ERROR=$?

      if test $ERROR -eq 0; then
        echo "server started"
      else
        cat "$PG_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=$PG_SOCKET_DIR --port=$PG_PORT brep
    ;;
esac

if test "$cmd" = "init"; then
  # Create brep DB if not exist.
  #
  psql --host=$PG_SOCKET_DIR --port=$PG_PORT -c "" brep \
	 1>>"$PG_OUT_FILE" 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=$PG_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

  # Populate brep DB if required.
  # psql --host=$PG_SOCKET_DIR --port=$PG_PORT -c "select count(1) ..." brep \
  #	 1>>"$PG_OUT_FILE" 2>&1
  # ...
fi