Skip to content

Commit 27bd2cb

Browse files
Add database management notes
1 parent f369be1 commit 27bd2cb

File tree

1 file changed

+60
-7
lines changed

1 file changed

+60
-7
lines changed

docs/deployment.md

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ This app can be deployed to a web server using the script [`deploy.sh`](../deplo
4242
1. Configure the `.env` file as described below.
4343
2. Run the deployment script: `sudo bash -x deploy.sh`
4444
3. Configure the database
45+
4. Run database migrations
4546

4647
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).
4748

@@ -147,8 +148,6 @@ We can list users (i.e. database "roles") using [the `\du` command](https://www
147148
psql sort --command "\du"
148149
```
149150

150-
151-
152151
## Grant permissions
153152

154153
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
182181

183182
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.
184183

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+
DROP TABLE IF EXISTS table_name CASCADE;
234+
```
235+
185236
# Security
186237

187238
## SSL Certificates
188239

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.
190241

191242
We can install the private key
192243

@@ -218,27 +269,29 @@ To use the [Django management tool](https://docs.djangoproject.com/en/5.1/ref/dj
218269
sort_dir="/opt/sort"
219270
venv_dir="$sort_dir/venv"
220271
python="$venv_dir/bin/python"
272+
django_admin="$python $sort_dir/manage.py"
273+
221274
cd "$sort_dir"
222275
# Check the Django management tool works
223-
$python "$sort_dir"/manage.py version
276+
sudo $django_admin version
224277
```
225278

226279
View available commands
227280

228281
```bash
229-
$python "$sort_dir"/manage.py help
282+
sudo $django_admin help
230283
```
231284

232285
Migrate the database
233286

234287
```bash
235-
sudo $python manage.py migrate
288+
sudo $django_admin migrate
236289
```
237290

238291
Create a super-user
239292

240293
```bash
241-
sudo $python manage.py createsuperuser
294+
sudo $django_admin createsuperuser
242295
```
243296

244297
Load data

0 commit comments

Comments
 (0)