You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/deployment.md
+60-7Lines changed: 60 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,6 +42,7 @@ This app can be deployed to a web server using the script [`deploy.sh`](../deplo
42
42
1. Configure the `.env` file as described below.
43
43
2. Run the deployment script: `sudo bash -x deploy.sh`
44
44
3. Configure the database
45
+
4. Run database migrations
45
46
46
47
We can run commands and Bash scripts as the superuser (`root`) using the [`sudo` command](https://manpages.ubuntu.com/manpages/noble/en/man8/sudo.8.html).
47
48
@@ -147,8 +148,6 @@ We can list users (i.e. database "roles") using [the `\du` command](https://www
147
148
psql sort --command "\du"
148
149
```
149
150
150
-
151
-
152
151
## Grant permissions
153
152
154
153
We must allow this user the minimum necessary privileges to operate the web app. We authorise the user using the PostgreSQL [grant statement](https://www.postgresql.org/docs/current/sql-grant.html), which we execute in the `psql` tool.
@@ -182,11 +181,63 @@ ALTER DEFAULT PRIVILEGES FOR USER sort GRANT SELECT, INSERT, UPDATE, DELETE ON T
182
181
183
182
On our PostgreSQL instance, this should create a database named `sort` with a user named `sort` that has all the necessary permissions on the `sort` schema to create, modify, and drop tables and read/write data to those tables.
184
183
184
+
# Database administration
185
+
186
+
To open the [`psql` terminal](https://www.postgresql.org/docs/16/app-psql.html), use the [`dbshell`](https://docs.djangoproject.com/en/5.1/ref/django-admin/#dbshell) command. We can pass a command using the [`--` option](https://docs.djangoproject.com/en/5.1/ref/django-admin/#cmdoption-dbshell-0). We need to set up the shell as follows:
187
+
188
+
```bash
189
+
sort_dir="/opt/sort"
190
+
venv_dir="$sort_dir/venv"
191
+
python="$venv_dir/bin/python"
192
+
psql="$python manage.py dbshell --"
193
+
194
+
cd"$sort_dir"
195
+
```
196
+
197
+
To view the available PostgreSQL commands:
198
+
199
+
```bash
200
+
sudo $psql -c "\?"
201
+
```
202
+
203
+
The `psql` command invokes the Postgres shell:
204
+
205
+
```bash
206
+
$ sudo $psql
207
+
sort=>
208
+
```
209
+
210
+
Once you're in the `psql` shell, you can exit using the `\q` command.
211
+
212
+
## Manage tables
213
+
214
+
### Run schema migrations
215
+
216
+
Run the migrations command using [django-admin](https://docs.djangoproject.com/en/5.1/ref/django-admin/)
217
+
218
+
```bash
219
+
sudo $python manage.py migrate
220
+
```
221
+
222
+
### List tables
223
+
224
+
```bash
225
+
sudo $psql -c "\dt"
226
+
```
227
+
228
+
### Drop tables
229
+
230
+
You can delete tables using the [`DROP TABLE` syntax](https://www.postgresql.org/docs/current/sql-droptable.html).
231
+
232
+
```sql
233
+
DROPTABLE IF EXISTS table_name CASCADE;
234
+
```
235
+
185
236
# Security
186
237
187
238
## SSL Certificates
188
239
189
-
See: ITS Wiki [SSL Certificates/Howto](https://itswiki.shef.ac.uk/wiki/SSL_Certificates/Howto) for the commands to generate a Certificate Signing Request (CSR) using [OpenSSL](https://docs.openssl.org/3.3/man1/openssl-req/#options)with an unencrypted private key.
240
+
See: ITS Wiki [SSL Certificates/Howto](https://itswiki.shef.ac.uk/wiki/SSL_Certificates/Howto) for the commands to generate a Certificate Signing Request (CSR) using [OpenSSL](https://docs.openssl.org/3.3/man1/openssl-req/#options)with an unencrypted private key.
190
241
191
242
We can install the private key
192
243
@@ -218,27 +269,29 @@ To use the [Django management tool](https://docs.djangoproject.com/en/5.1/ref/dj
0 commit comments