-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshell-hook.sh
147 lines (132 loc) · 8.58 KB
/
shell-hook.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/sh
echo "EXECUTING SHELL HOOK"
######################################################################
# Create a diretory for the generated artifacts #
# #
# ( This allows stacking Nix shell expressions. For example, nginx #
# will store its own "nginx" directory with logs, pidfile, etc. #
# ) #
######################################################################
export NIX_SHELL_DIR="${PWD}/_nix-shell"
######################################################################
# Put the PostgreSQL databases in the project diretory. #
######################################################################
export PGDATA="${NIX_SHELL_DIR}/postgres"
######################################################################
# If database is not initialized (i.e., $PGDATA directory does not #
# exist), then set it up. Seems superfulous given the cleanup step #
# above, but handy when one had to force reboot the iron. #
######################################################################
if ! test -d $PGDATA
then
#############################################################
# Init PostgreSQL #
# #
# NOTE `initdb` vs `createdb` #
# #
# + What's the difference between initdb and createdb #
# https://stackoverflow.com/questions/50210158/ #
# #
# + https://www.postgresql.org/docs/current/app-initdb.html #
# #
#############################################################
pg_ctl initdb -D $PGDATA
#############################################################
# PORT ALREADY IN USE #
#############################################################
# #
# If another `nix-shell` is running with a PostgreSQL #
# instance, the logs will show complaints that the #
# default port 5432 is already in use. Edit the line #
# below with a different port number, uncomment it, #
# and try again. #
# #
#############################################################
# sed -i "s|^#port.*$|port = 5433|" $PGDATA/postgresql.conf
fi
########################################################################
# Configure and start PostgreSQL #
# ==================================================================== #
# #
# Setting all necessary configuration options via `pg_ctl` (which #
# is basically a wrapper around `postgres`) instead of editing #
# `postgresql.conf` directly with `sed`. See docs: #
# #
# + https://www.postgresql.org/docs/current/app-pg-ctl.html #
# + https://www.postgresql.org/docs/current/app-postgres.html #
# #
# See more on the caveats at #
# https://discourse.nixos.org/t/how-to-configure-postgresql-declaratively-nixos-and-non-nixos/4063/1
# but recapping out of paranoia: #
# #
# > use `SHOW` commands to check the options because `postgres -C` #
# > "_returns values from postgresql.conf_" (which is not changed by #
# > supplying the configuration options on the command line) and #
# > "_it does not reflect parameters supplied when the cluster was #
# > started._" #
# #
# OPTION SUMMARY #
# -------------------------------------------------------------------- #
# #
# + `unix_socket_directories` #
# #
# PostgreSQL will attempt to create a pidfile in #
# `/run/postgresql` by default, but it will fail as it #
# doesn't exist. By changing the configuration option #
# below, it will get created in $PGDATA. #
# #
# + `listen_addresses` #
# #
# In tandem with edits in `pg_hba.conf` (see #
# `HOST_COMMON` below), it configures PostgreSQL to #
# allow remote connections (otherwise only `localhost` #
# will get authorized and the rest of the traffic #
# will be discarded). #
# #
# NOTE: the edit to `pga_hba.conf` needs to come #
# **before** `pg_ctl start` (or the service #
# needs to be restarted otherwise), because then #
# the changes are not being reloaded. #
# #
# More info on setting up and troubleshooting remote #
# PosgreSQL connections (these are all mirrors of the #
# same text; again, paranoia): #
# #
# * connect to postgres server on google compute engine #
# https://stackoverflow.com/questions/24504680/ #
# #
# * How to connect to remote PostgreSQL server on google #
# compute engine? #
# https://stackoverflow.com/questions/47794979/ #
# #
# * https://medium.com/scientific-breakthrough-of-the-afternoon/configure-postgresql-to-allow-remote-connections-af5a1a392a38
# #
# * https://gist.github.com/toraritte/f8c7fe001365c50294adfe8509080201#file-configure-postgres-to-allow-remote-connection-md
# #
# + `log*` #
# #
# Setting up basic logging, to see remote connections #
# for example. #
# #
# See the docs for more: #
# https://www.postgresql.org/docs/current/runtime-config-logging.html
########################################################################
# !!!!!!!!!!!! These are only suitable for development.
# ! INSECURE ! (Not sure if running a production server
# !!!!!!!!!!!! from `nix-shell` is a good idea anyway:)
HOST_COMMON="host\s\+all\s\+all"
sed -i "s|^$HOST_COMMON.*127.*$|host all all 0.0.0.0/0 trust|" $PGDATA/pg_hba.conf
sed -i "s|^$HOST_COMMON.*::1.*$|host all all ::/0 trust|" $PGDATA/pg_hba.conf
pg_ctl \
-D $PGDATA \
-l $PGDATA/postgres.log \
-o "-c unix_socket_directories='$PGDATA'" \
-o "-c listen_addresses='*'" \
-o "-c log_destination='stderr'" \
-o "-c logging_collector=on" \
-o "-c log_directory='log'" \
-o "-c log_filename='postgresql-%Y-%m-%d_%H-%M-%S.log'" \
-o "-c log_min_messages=info" \
-o "-c log_min_error_statement=info" \
-o "-c log_connections=on" \
start