# Workshop: Postgres
# Situation 1: without volume
- create Postgres container
docker run -d -e POSTGRES_PASSWORD=1234 postgres:14.1
1
- go inside container
docker ps
1
output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
407e03ea3d60 postgres:14.1 "docker-entrypoint.s…" 6 seconds ago Up 2 seconds 5432/tcp youthful_driscoll
1
2
2
docker exec -it 407 bash
1
- connect to Postgres (for Postgres container postgres is default user. For more information please read postgres Docker Hub (opens new window))
psql -U postgres
1
- create db
CREATE DATABASE mydb;
1
- list db
\l
1
output
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
mydb | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- use db
\c mydb
1
- create table
CREATE TABLE users(
name VARCHAR(50) PRIMARY KEY,
age INT NOT NULL
);
1
2
3
4
2
3
4
\dt
1
output
List of relations
Schema | Name | Type | Owner
--------+-------+-------+----------
public | users | table | postgres
(1 row)
1
2
3
4
5
2
3
4
5
- insert a user
INSERT INTO users (name, age)
VALUES ('Chaky', 22);
1
2
2
SELECT * FROM users;
1
output
name | age
-------+-----
Chaky | 22
(1 row)
1
2
3
4
2
3
4
- exit from psql
exit
1
- exit from container
exit
1
- remove container
docker rm 407 -f
1
- make sure that our data already gone
docker run -d -e POSTGRES_PASSWORD=1234 postgres:14.1
1
output
95036a5a81525b5bab2feb5aec44dff76b936af20094d835c675b873272c3fbb
1
- next
docker exec -it 950 bash
1
psql -U postgres
1
- list databases
\l
1
output
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
- exit and remove container
exit
exit
docker rm 950 -f
1
2
3
2
3
# Situation 2: with volume
- create Postgres container
docker run -d -e POSTGRES_PASSWORD=1234 -v postgres-volume:/var/lib/postgresql/data postgres:14.1
1
output
e2d46d3faafe2790f5db67335d8566e153ffca857d667a5513b53271adc844de
1
- list volume
docker volume ls
1
output
DRIVER VOLUME NAME
local postgres-volume
1
2
2
- create database, table and insert data
docker exec -it e2d bash
1
psql -U postgres
1
CREATE DATABASE mydb;
1
\c mydb
1
CREATE TABLE users(
name VARCHAR(50) PRIMARY KEY,
age INT NOT NULL
);
1
2
3
4
2
3
4
INSERT INTO users (name, age)
VALUES ('Chaky', 22);
1
2
2
SELECT * FROM users;
1
output
name | age
-------+-----
Chaky | 22
(1 row)
1
2
3
4
2
3
4
- remove container and create again
exit
exit
docker rm e2d -f
docker run -d -e POSTGRES_PASSWORD=1234 -v postgres-volume:/var/lib/postgresql/data postgres:14.1
1
2
3
4
2
3
4
output
d556ada570b54aec1de343b04ff762104c7cda82aee2da6f1dd840274d5a7d4f
1
- go inside container
docker exec -it d55 bash
psql -U postgres
\c mydb
1
2
3
2
3
SELECT * FROM users;
1
output
name | age
-------+-----
Chaky | 22
(1 row)
1
2
3
4
2
3
4
- remove container, volume, and finish this lab
exit
exit
docker rm d55 -f
1
2
3
2
3
docker volume rm postgres-volume
1