0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【所要時間1分】テストデータの入ったpostgresqlコンテナ環境を爆速で構築する

Last updated at Posted at 2021-05-26

この記事では、テストデータが入った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)


参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?