Skip to content

Commit 4204f48

Browse files
author
David Arevalo
committed
update Readme
1 parent 733dfd4 commit 4204f48

7 files changed

+109
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
.DS_Store
12
postgres-data/*

README.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
1-
# docker_postgres_with_data
1+
## Creating and filling a Postgres DB with Docker Compose
2+
3+
This project create tables and fill the tables in a Postgres data base. The tables and data that create are in sql/ folder
4+
## ER Diagram
5+
The ER Diagram of DB is this
6+
![ER Diagram](https://github.com/jdaarevalo/docker_postgres_with_data/images/ER_Diagram.png)
7+
8+
## ER Diagram
9+
Set the variables in `sql/fill_tables.sql`
10+
| Parameter | Description |
11+
| ------ | ------ |
12+
| number_of_sales | Number of rows to create in the table sale |
13+
| number_of_users | Number of rows to create in the table users |
14+
| number_of_products | Number of rows to create in the table products |
15+
| number_of_stores | Number of rows to create in the table sale |
16+
| number_of_coutries | Number of rows to create in the table countries |
17+
| number_of_cities | Number of rows to create in the table cities |
18+
| number_of_status_names | Number of rows to create in the table status_names |
19+
| start_date | Date of the first sale |
20+
| end_date | Date of the last sale |
21+
22+
## Params
23+
If want change this params in `docker-compose.yaml`
24+
| Parameter | Description |
25+
| ------ | ------ |
26+
| POSTGRES_USER | The Postgres user to connect **postgres** |
27+
| POSTGRES_PASSWORD | The Postgres password to connect **postgres** |
28+
| POSTGRES_DB | The Postgres password to connect **postgres** |
29+
| port | The port mapped by Postgres is **5432** in your container. In this example, use the port **5438** on the host machine |
30+
31+
## Run
32+
```sh
33+
$ docker-compose up
34+
```
35+

docker-compose.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ services:
1616
- ./postgres-data:/var/lib/postgresql/data
1717
# copy the sql script to create tables
1818
- ./sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql
19-
#command:
20-
#- |
21-
#psql -U postgres test -f '/sql/create_tables.sql'
19+
# copy the sql script to fill tables
20+
- ./sql/fill_tables.sql:/docker-entrypoint-initdb.d/fill_tables.sql

images/ER_Diagram.png

405 KB
Loading

images/print_execution.png

3.28 MB
Loading

images/print_query.png

905 KB
Loading

sql/fill_tables.sql

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
-- Set params
2+
set session my.number_of_sales = '2000000';
3+
set session my.number_of_users = '500000';
4+
set session my.number_of_products = '300';
5+
set session my.number_of_stores = '500';
6+
set session my.number_of_coutries = '100';
7+
set session my.number_of_cities = '30';
8+
set session my.status_names = '5';
9+
set session my.start_date = '2019-01-01 00:00:00';
10+
set session my.end_date = '2020-02-01 00:00:00';
11+
12+
-- load the pgcrypto extension to gen_random_uuid ()
13+
CREATE EXTENSION pgcrypto;
14+
15+
-- Filling of products
16+
INSERT INTO product
17+
select id, concat('Product ', id)
18+
FROM GENERATE_SERIES(1, current_setting('my.number_of_products')::int) as id;
19+
20+
-- Filling of countries
21+
INSERT INTO country
22+
select id, concat('Country ', id)
23+
FROM GENERATE_SERIES(1, current_setting('my.number_of_coutries')::int) as id;
24+
25+
-- Filling of cities
26+
INSERT INTO city
27+
select id
28+
, concat('City ', id)
29+
, floor(random() * (current_setting('my.number_of_coutries')::int) + 1)::int
30+
FROM GENERATE_SERIES(1, current_setting('my.number_of_cities')::int) as id;
31+
32+
-- Filling of stores
33+
INSERT INTO store
34+
select id
35+
, concat('Store ', id)
36+
, floor(random() * (current_setting('my.number_of_cities')::int) + 1)::int
37+
FROM GENERATE_SERIES(1, current_setting('my.number_of_stores')::int) as id;
38+
39+
-- Filling of users
40+
INSERT INTO users
41+
select id
42+
, concat('User ', id)
43+
FROM GENERATE_SERIES(1, current_setting('my.number_of_users')::int) as id;
44+
45+
-- Filling of users
46+
INSERT INTO status_name
47+
select status_name_id
48+
, concat('Status Name ', status_name_id)
49+
FROM GENERATE_SERIES(1, current_setting('my.status_names')::int) as status_name_id;
50+
51+
-- Filling of sales
52+
INSERT INTO sale
53+
select gen_random_uuid ()
54+
, round(CAST(float8 (random() * 10000) as numeric), 3)
55+
, TO_TIMESTAMP(start_date, 'YYYY-MM-DD HH24:MI:SS') +
56+
random()* (TO_TIMESTAMP(end_date, 'YYYY-MM-DD HH24:MI:SS')
57+
- TO_TIMESTAMP(start_date, 'YYYY-MM-DD HH24:MI:SS'))
58+
, floor(random() * (current_setting('my.number_of_products')::int) + 1)::int
59+
, floor(random() * (current_setting('my.number_of_users')::int) + 1)::int
60+
, floor(random() * (current_setting('my.number_of_stores')::int) + 1)::int
61+
FROM GENERATE_SERIES(1, current_setting('my.number_of_sales')::int) as id
62+
, current_setting('my.start_date') as start_date
63+
, current_setting('my.end_date') as end_date;
64+
65+
-- Filling of order_status
66+
INSERT INTO order_status
67+
select gen_random_uuid ()
68+
, date_sale + random()* (date_sale + '5 days' - date_sale)
69+
, sale_id
70+
, floor(random() * (current_setting('my.status_names')::int) + 1)::int
71+
from sale;

0 commit comments

Comments
 (0)