この記事では、テストデータが入ったpostgresコンテナ環境を構築し、postgresの学習を行うための環境の構築手順について記載する
テストデータが入ったdokcer image build
docker build -t test -<<EOF
FROM postgres:13.3-alpine
RUN echo 'wget https://www.postgresqltutorial.com/wp-content/uploads/2019/05/dvdrental.zip' >> /docker-entrypoint-initdb.d/start.sh
RUN echo 'unzip dvdrental.zip' >> /docker-entrypoint-initdb.d/start.sh
RUN echo 'pg_restore -h localhost -p 5432 -U postgres -d dvdrental dvdrental.tar' >> /docker-entrypoint-initdb.d/start.sh
EXPOSE 5432
CMD ["postgres"]
EOF
コンテナ環境起動
docker run -i --rm -d\
--name test \
-p 15432:5432 \
-v postgres-tmp:/var/lib/postgresql/data \
-e POSTGRES_HOST_AUTH_METHOD=trust \
test
コンテナ環境ログイン
docker exec -it tesh sh
/# psql -U postgres
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
dvdrental | 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)
\c dvdrental
You are now connected to database "dvdrental" as user "postgres".
dvdrental-# \dt
List of relations
Schema | Name | Type | Owner
--------+---------------+-------+----------
public | actor | table | postgres
public | address | table | postgres
public | category | table | postgres
public | city | table | postgres
public | country | table | postgres
public | customer | table | postgres
public | film | table | postgres
public | film_actor | table | postgres
public | film_category | table | postgres
public | inventory | table | postgres
public | language | table | postgres
public | payment | table | postgres
public | rental | table | postgres
public | staff | table | postgres
public | store | table | postgres
(15 rows)
参考