Skip to content

Commit

Permalink
Support DB service in docker (#10)
Browse files Browse the repository at this point in the history
* Add initial version of compose

* Enable volume for pre-init scripts

* Make storage persistent for dev env

* Provide healthcheck and deeper infra configuration

* Fix HC user and DB

* Finetune DB cluster settings

* Supply pgadmin service

* Fix pg_stat_statements not being available

* Update docs
  • Loading branch information
kostmetallist authored Dec 19, 2023
1 parent 452ae39 commit 3f1482f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
68 changes: 68 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
version: "3.8"
services:

postgres:
image: postgres:14.10-alpine3.19
container_name: crack-db
command:
- "postgres"
- "-c"
- "max_connections=50"
- "-c"
- "shared_preload_libraries=pg_stat_statements"
- "-c"
- "pg_stat_statements.max=10000"
- "-c"
- "pg_stat_statements.track=all"
environment:
POSTGRES_DB: "crack"
POSTGRES_USER: "crack_user"
POSTGRES_PASSWORD: "crack_pass"
PGDATA: "/var/lib/postgresql/data/pgdata"
volumes:
- ./rearguarde/configuration/init_scripts:/docker-entrypoint-initdb.d
- local_crack_storage:/var/lib/postgresql/data
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U crack_user -d crack"]
interval: 15s
timeout: 5s
retries: 5
start_period: 10s
restart: on-failure:2
deploy:
resources:
limits:
cpus: '1'
memory: 2G
networks:
- main

pgadmin:
image: dpage/pgadmin4:8.1
container_name: crack-admin-dash
environment:
PGADMIN_DEFAULT_EMAIL: "admin@example.com"
PGADMIN_DEFAULT_PASSWORD: "crack_admin_pass"
PGADMIN_CONFIG_SERVER_MODE: "False"
volumes:
- local_pgadmin_storage:/var/lib/pgadmin
ports:
- "5433:80"
restart: unless-stopped
deploy:
resources:
limits:
cpus: '0.5'
memory: 1G
networks:
- main

volumes:
local_crack_storage:
local_pgadmin_storage:

networks:
main:
driver: bridge
24 changes: 19 additions & 5 deletions docs/db-postgresql-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,23 @@ For the time being, a dedicated DB-user will be established for connection to th
Before running python code for the database creation, one needs to create user via `psql` or
`pgAdmin4` utilities. Here, the command line approach will be enlightened (i.e. through `psql`).

## Linux notes
## Option 1: containerized

### User initiation
It is straighforward to spin up a database container along with pgAdmin dashboard in an isolated environment.
Simply execute

```shell
docker-compose --project-name="taburette-db-layer" up -d
```

while CWD in the same directory as `docker-compose.yaml`. You'll have two services with necessary
ports exposed to the host.

## Option 2: system-wide

### Linux notes

#### User initiation

In Unix, PostgreSQL uses role-based authentication, which is enabled by default so instead of
logging in as a superuser (`postgres` initially), administrator should create another role with
Expand Down Expand Up @@ -46,7 +60,7 @@ It will create a user with an ability to create any databases inside the DBMS en
it can, use the `\du` command to display a table with roles configured in PostgreSQL and their
attributes.

### Authentication settings
#### Authentication settings

For making possible to login as a newly created `crack_user`, you need to edit the
`/etc/postgresql/10/main/pg_hba.conf` to specify the authentication method for the user. Add the
Expand All @@ -62,7 +76,7 @@ For the changes to be applied one should consider restarting the `postgresql` da
`sudo service postgresql restart`. Finally, by the `psql -U crack_user -W -d medium` a newly created
user can connect to the intermediate database.

### Database origination
#### Database origination

Now it is time to 'crack' database to be created by the `crack_user`. Execute

Expand All @@ -73,7 +87,7 @@ CREATE DATABASE "crack" ENCODING 'UTF8';
You can assure database is created by entering `\l` command. Since this moment, you can access
the database from python's code.

## Windows notes
### Windows notes

You can face an issue with 866 and 1251 encoding pages discrepancies in the Windows `cmd` and `psql`
if the default OS locale is not US English. To avoid such problem, before running the `psql`
Expand Down
1 change: 1 addition & 0 deletions rearguarde/configuration/init_scripts/setup_extensions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create extension if not exists pg_stat_statements;

0 comments on commit 3f1482f

Please sign in to comment.