-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathsetup
More file actions
executable file
·147 lines (118 loc) · 4.65 KB
/
setup
File metadata and controls
executable file
·147 lines (118 loc) · 4.65 KB
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
#!/usr/bin/env bash
set -o errexit
set -o errtrace
set -o nounset
set -o pipefail
# Read the URL from the 'latest' file
LATEST=`cat latest`
# Extract the filename from the URL (e.g., postcodesio-2023.tar.gz)
LATEST_FILENAME=$(basename "$LATEST")
echo "Postcodes.io Database Setup Script"
echo "----------------------------------"
echo ""
echo "In order to create, setup and download your database we need Postgresql user credentials with superuser privileges to carry out some operations.\n"
echo "Your Postgresql user credentials will be used to:"
echo "- Create a new database"
echo "- Create a new Postgresql user and password for the new database. If user already exists this step is skipped"
echo "- Stream the latest pg_dump (at $LATEST) to your newly created database"
echo "- Apply read-only privileges for the new user to the new database"
echo ""
# --- Configuration Section (Updated for Non-Interactive Mode) ---
# 1. Postgres Superuser
if [ -z "${POSTGRES_SUPERUSER:-}" ]; then
read -p "Postgresql Superuser (for database creation) [default: postgres]:" POSTGRES_SUPERUSER
fi
POSTGRES_SUPERUSER=${POSTGRES_SUPERUSER:-postgres}
# 2. App User
if [ -z "${POSTGRES_USER:-}" ]; then
read -p "New Database Username (for readonly access) [default: postcodesio]:" POSTGRES_USER
fi
POSTGRES_USER=${POSTGRES_USER:-postcodesio}
# 3. App Password
if [ -z "${POSTGRES_PASSWORD:-}" ]; then
read -p "Password for new user [default: secret]:" POSTGRES_PASSWORD
fi
POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-secret}
# 4. Host
if [ -z "${POSTGRES_HOST:-}" ]; then
read -p "Postgresql Host [default: localhost]:" POSTGRES_HOST
fi
POSTGRES_HOST=${POSTGRES_HOST:-localhost}
# 5. Database Name
# Handle mismatch: user might set POSTGRES_DATABASE, script expects POSTGRES_DB
if [ -z "${POSTGRES_DB:-}" ]; then
# Fallback to POSTGRES_DATABASE if set, otherwise prompt
if [ -n "${POSTGRES_DATABASE:-}" ]; then
POSTGRES_DB="$POSTGRES_DATABASE"
else
read -p "Database Name [default: postcodesiodb]:" POSTGRES_DB
fi
fi
POSTGRES_DB=${POSTGRES_DB:-postcodesiodb}
# 6. Port
if [ -z "${POSTGRES_PORT:-}" ]; then
read -p "Postgresql Port: [default: 5432]:" POSTGRES_PORT
fi
POSTGRES_PORT=${POSTGRES_PORT:-5432}
PSQL="psql --username=$POSTGRES_SUPERUSER --host=$POSTGRES_HOST --port=$POSTGRES_PORT"
# --- Database Logic ---
# Create postgres user
# With thanks to Erwin Brandstetter (http://stackoverflow.com/questions/8092086/create-postgresql-role-user-if-it-doesnt-exist)
echo "\nCreating new user $POSTGRES_USER if it does not already exist..."
$PSQL --quiet --command="DO
\$body\$
BEGIN
IF NOT EXISTS (
SELECT *
FROM pg_catalog.pg_user
WHERE usename = '$POSTGRES_USER') THEN
CREATE ROLE $POSTGRES_USER LOGIN PASSWORD '$POSTGRES_PASSWORD';
END IF;
END
\$body\$"
echo "Done\n"
sleep 1
# Create Database
echo "Creating new database called $POSTGRES_DB..."
if createdb -p $POSTGRES_PORT -h $POSTGRES_HOST -U $POSTGRES_SUPERUSER $POSTGRES_DB
then
echo "Done\n"
else
echo "Failed to create database. Please check the error message and take action"
exit 1
fi
sleep 1
# --- Import Logic (Updated for Local File Check) ---
echo "Preparing to import dataset..."
if [ -f "$LATEST_FILENAME" ]; then
# Case A: Local file exists
echo "Found local file: $LATEST_FILENAME"
echo "Importing from local file...\n"
echo "Please wait a few minutes for this operation to finish...\n"
# Check if the file is gzipped based on extension or just try gunzip
cat "$LATEST_FILENAME" | gunzip -c | $PSQL --dbname=$POSTGRES_DB -v ON_ERROR_STOP=1 --quiet
else
# Case B: Local file not found, use original streaming download
echo "Local file '$LATEST_FILENAME' not found."
echo "Downloading latest dataset from $LATEST and importing to Postgresql (Streaming)...\n"
echo "Please wait a few minutes for this operation to finish...\n"
wget -O - $LATEST | gunzip -c | $PSQL --dbname=$POSTGRES_DB -v ON_ERROR_STOP=1 --quiet
fi
echo "Done\n"
# Grant Permissions on App User
echo "Granting read (SELECT) permissions on new user..."
COMMAND="GRANT CONNECT ON DATABASE $POSTGRES_DB TO $POSTGRES_USER; \
GRANT SELECT ON ALL TABLES IN SCHEMA public TO $POSTGRES_USER; \
"
if echo $COMMAND | $PSQL --dbname=$POSTGRES_DB
then
echo "Done\n"
else
echo "Failed to grant privileges. Please check the error message and take action"
exit 1
fi
echo "Database setup is complete"
echo ""
echo "Run the API server with the right database credentials to proceed:"
echo "POSTGRES_USER=$POSTGRES_USER POSTGRES_DATABASE=$POSTGRES_DB POSTGRES_PASSWORD=$POSTGRES_PASSWORD POSTGRES_HOST=$POSTGRES_HOST npm start"
echo "Alternatively update those credentials in your .env file"