2018-09-25 15:46:05 -04:00
|
|
|
# Database setup
|
2018-08-01 09:17:20 -04:00
|
|
|
|
2018-09-25 15:46:05 -04:00
|
|
|
Initial setup, on first connection (replacing hostname, username, port, dbname as required):
|
2018-08-01 09:17:20 -04:00
|
|
|
|
|
|
|
```bash
|
2018-09-25 15:46:05 -04:00
|
|
|
$ psql "host={hostname} user={username} port={port} sslmode=require dbname=postgres"
|
2018-08-01 09:17:20 -04:00
|
|
|
> create database colouringlondon;
|
|
|
|
> \c colouringlondon
|
|
|
|
> create extension postgis;
|
|
|
|
> create extension pgcrypto;
|
|
|
|
> \q
|
2018-09-29 13:14:21 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
To run all up migrations:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ ls ./*.up.sql 2>/dev/null | while read -r migration; do psql < $migration; done;
|
|
|
|
```
|
|
|
|
|
|
|
|
Or all down migrations in reverse order:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ ls -r ./*.down.sql 2>/dev/null | while read -r migration; do psql < $migration; done;
|
2018-08-01 09:17:20 -04:00
|
|
|
```
|
2018-08-08 04:05:58 -04:00
|
|
|
|
2018-09-25 15:46:05 -04:00
|
|
|
Create an app user:
|
2018-08-08 04:05:58 -04:00
|
|
|
|
|
|
|
```sql
|
|
|
|
-- role for server-side of front end (HTTP POST)
|
2018-09-25 15:46:05 -04:00
|
|
|
CREATE ROLE appusername WITH LOGIN;
|
2018-08-08 04:05:58 -04:00
|
|
|
-- create/update, authenticate and authorise users
|
2018-10-21 15:47:44 -04:00
|
|
|
GRANT SELECT, UPDATE, INSERT, DELETE ON TABLE users, user_sessions TO appusername;
|
2018-09-25 15:46:05 -04:00
|
|
|
-- join users against categories and access levels
|
|
|
|
GRANT SELECT ON TABLE user_access_levels, user_categories TO appusername;
|
2018-08-08 04:05:58 -04:00
|
|
|
-- read/write building data
|
2018-09-25 15:46:05 -04:00
|
|
|
GRANT SELECT, UPDATE ON TABLE buildings TO appusername;
|
2018-09-29 13:14:21 -04:00
|
|
|
GRANT SELECT, INSERT, DELETE ON TABLE building_user_likes TO appusername;
|
2018-08-08 04:05:58 -04:00
|
|
|
-- read geometry data
|
2018-09-25 15:46:05 -04:00
|
|
|
GRANT SELECT ON TABLE geometries TO appusername;
|
2018-08-08 04:05:58 -04:00
|
|
|
-- read/append to logs
|
2018-09-25 15:46:05 -04:00
|
|
|
GRANT SELECT, INSERT ON TABLE logs to appusername;
|
2018-08-08 04:05:58 -04:00
|
|
|
-- use id sequences
|
2018-09-25 15:46:05 -04:00
|
|
|
GRANT USAGE ON ALL SEQUENCES IN SCHEMA public to appusername;
|
2018-08-08 04:05:58 -04:00
|
|
|
-- use postgis/pgcrypto functions
|
2018-09-25 15:46:05 -04:00
|
|
|
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO appusername;
|
2018-08-08 04:05:58 -04:00
|
|
|
```
|
|
|
|
|
2018-09-25 15:46:05 -04:00
|
|
|
Set or update passwords:
|
2018-08-08 04:05:58 -04:00
|
|
|
|
|
|
|
```bash
|
2018-09-25 15:46:05 -04:00
|
|
|
psql -c "ALTER USER appusername WITH PASSWORD 'longsecurerandompassword';"
|
2018-08-08 04:05:58 -04:00
|
|
|
```
|