Docker使って、こちらのサンプルDBをDockerのPostgreSQLに投入する手順です。
PostgreSQLそのものの機能というより、別サービスとの接続確認などで使います。
準備
# ディレクトリ作成 && 移動
mkdir pg-sample && cd pg-sample
# サンプルDBのダウンロード
wget https://sp.postgresqltutorial.com/wp-content/uploads/2019/05/dvdrental.zip && unzip dvdrental.zip
コンテナ起動
PASSWORD=passw0rd
VERSION=12.4
docker run --rm -d --name sampledb \
-p 5432:5432 \
-e POSTGRES_PASSWORD=${PASSWORD} \
-v $(pwd):/tmp/data \
postgres:${VERSION}-alpine
サンプルDBのデータ投入
docker exec sampledb psql -U postgres -c "create database dvdrental"
docker exec sampledb pg_restore -U postgres -d dvdrental /tmp/data/dvdrental.tar
使ってみる
psql
で繋いでみる。
$ docker exec -it sampledb psql -U postgres
psql (12.4 (Debian 12.4-1.pgdg100+1))
Type "help" for help.
postgres=# \c dvdrental
You are now connected to database "dvdrental" as user "postgres".
dvdrental=# \d
List of relations
Schema | Name | Type | Owner
--------+----------------------------+----------+----------
public | actor | table | postgres
public | actor_actor_id_seq | sequence | postgres
public | actor_info | view | postgres
public | address | table | postgres
public | address_address_id_seq | sequence | postgres
public | category | table | postgres
5432ポートで繋ぐことも可能です。
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
02857907c9a9 postgres:12.4-alpine "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 0.0.0.0:5432->5432/tcp sampledb
片付け
docker rm -f sampledb