動作環境
mac osx 10.15.7
Docker 20.10.5
テーブル作成までの流れ
$ docker network create db
$ docker run --name db -p 5432:5432 --network=db -v "$PWD:/var/lib/postgresql/data" -e POSTGRES_PASSWORD=password -d postgres:alpine
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
56014f6ccde1 postgres:alpine "docker-entrypoint.s…" 15 seconds ago Up 10 seconds 0.0.0.0:5432->5432/tcp db
$ docker run -it --rm --network=db postgres:alpine psql -h db -U postgres
--'password'を入力
postgres=# create database student;
CREATE DATABASE
postgres=# \c student
You are now connected to database "student" as user "postgres".
student=# create table student(id BIGSERIAL PRIMARY KEY, name TEXT);
student=# \dt
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
public | student | table | postgres
(1 row)
student=# insert into student (name) values ('Alex'),('Maria');
INSERT 0 2
student=# select * from student;
id | name
----+-------
1 | Alex
2 | Maria
# おまけ
使わなくなったら消しましょう
postgres=# drop database student;
DROP DATABASE
postgres=# exit
$ docker rm -f db
db
$ docker network rm db
db