-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b422f98
commit 896c66b
Showing
8 changed files
with
64 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
docker-compose-files/postgres/postgres-pgadmin/test-persistence.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import psycopg | ||
|
||
# Connect to an existing database | ||
with psycopg.connect("dbname=db-name host=localhost password='change-me' user=username") as connection: | ||
# Open a cursor to perform database operations | ||
with connection.cursor() as cursor: | ||
cursor.execute(""" | ||
CREATE TABLE IF NOT EXISTS tests ( | ||
id UUID PRIMARY KEY, | ||
name VARCHAR(255) | ||
)""") | ||
cursor.execute(""" | ||
INSERT INTO public.tests | ||
VALUES (gen_random_uuid(), %(name)s) | ||
""", { | ||
"name": "Mohammad Jawad (Kasir)" | ||
}) | ||
cursor.execute(""" | ||
SELECT * | ||
FROM public.tests | ||
""") | ||
|
||
record = cursor.fetchone() | ||
|
||
print(record) | ||
|
||
# Make the changes to the database persistent | ||
connection.commit() |
File renamed without changes
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Docker volumes | ||
|
||
How you ever wondered how docker volumes work? Let's jump into it real quick. | ||
|
||
## Mechanics behind docker volumes | ||
|
||
- A special type of storage in docker. | ||
- Designed to **persist** data independent of the container lifecycle. | ||
- The idea is that when a container stops, crashes, or is deleted, the data stored in the volume remains intact **as long as the volume itself is not deleted**. | ||
- Gives you data isolation. | ||
- Bind mount: | ||
- Directly maps a host (your local system filesystem) directory, into a docker volume. | ||
- Volume: | ||
- Is managed by docker and stored in docker's storage directory (e.g., `/var/lib/docker/volumes` on Linux). | ||
|
||
## Compose files | ||
|
||
- E.g. in [this compose file](../docker-compose-files/postgres/postgres-pgadmin/compose.yml) we Map a named volume (i.e. `postgres`) to the container path `/var/lib/postgresql/data/pgdata`. | ||
|
||
That is where PostgreSQL stores its data files by default ([ref](https://github.com/docker-library/docs/blob/master/postgres/README.md#pgdata)). This ensures PostgreSQL's database files are written to the volume. | ||
|
||
- To test it: | ||
1. Run [this python script](../docker-compose-files/postgres/postgres-pgadmin/test-persistence.py). | ||
2. `docker compose down`. Do NOT pass `-v` flag since it will delete the volume along side the data. | ||
3. `docker compose up -d`. | ||
4. Check your database to see if everything is there. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters