Dockerを利用したPostgreSQLの環境構築
当ページのリンクには広告が含まれています。

Dockerを用いてPostgreSQLの環境構築を行います。
また、公式サンプルのデータも追加いたします。
目次
Dockerfileの作成
PostgreSQL用のDockerfileを作成します。
sample-dbとして、マウント用のフォルダを作成しました。
FROM postgres:11-alpine
ENV LANG ja_JP.utf8
RUN mkdir /var/lib/postgres/data/sample-dbdocker-compose.ymlの作成
docker-compose.ymlを作成します。
version: '3'
services:
db:
container_name: postgres
build: .
ports:
- 5433:5432
volumes:
- ./db:/var/lib/postgresql/data
- ./sample-db:/var/lib/postgresql/data/sample-db
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: admindocker-composeの実行
$ docker-compose build
$ docker-compose uppostgrsqlへ入る
$ docker exec -it postgres bashshellへ入ったらpsqlで入ります。
# psql -U adminサンプルデータの準備
dvdrentalのデータベースを作成sample/dvdrental.tarを取り込む
の手順で、サンプルデータを準備します。
CREATE DATABASE dvdrentalデータベースを作成し
$ pg_restore -U admin -d dvdrental /var/lib/postgresql/data/sample-db/dvdrental.tarpg_restoreでデータを読み込み
https://www.postgresql.jp/docs/9.0/app-pgrestore.html
\c dvdrental\cでdvdrentalデータベースに移動、\dtでテーブルを確認すると
dvdrental=# \dt
List of relations
Schema | Name | Type | Owner
--------+---------------+-------+-------
public | actor | table | admin
public | address | table | admin
public | category | table | admin
public | city | table | admin
public | country | table | admin
public | customer | table | admin
public | film | table | admin
public | film_actor | table | admin
public | film_category | table | admin
public | inventory | table | admin
public | language | table | admin
public | payment | table | admin
public | rental | table | admin
public | staff | table | admin
public | store | table | admin
(15 rows)データが入ってますね!

データは上記形式です。
TablePlusから接続
外部ツールから接続できるように設定を行います。
今回は、TablePlusを使用します。
TablePlusを開いたらCreate a new connectionでPostgreSQLを選択
- Host 127.0.0.1
- Port 5433
- User admin
- Password admin
を入力

Testを実行して成功すれば完了です。


コメント