Dockerを利用したPostgreSQLの環境構築

当ページのリンクには広告が含まれています。
  • URLをコピーしました!

Dockerを用いてPostgreSQLの環境構築を行います。

また、公式サンプルのデータも追加いたします。

サンプルファイルはこちら

PostgreSQL Tutorial
PostgreSQL Sample Database This tutorial introduces you to a PostgreSQL sample database that you can use for learning and practicing with PostgreSQL.
目次

Dockerfileの作成

PostgreSQL用のDockerfileを作成します。

sample-dbとして、マウント用のフォルダを作成しました。

FROM postgres:11-alpine
ENV LANG ja_JP.utf8
RUN mkdir /var/lib/postgres/data/sample-db

docker-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: admin

docker-composeの実行

$ docker-compose build
$ docker-compose up

postgrsqlへ入る

$ docker exec -it postgres bash

shellへ入ったらpsqlで入ります。

# psql -U admin

サンプルデータの準備

PostgreSQL Tutorial
PostgreSQL Sample Database This tutorial introduces you to a PostgreSQL sample database that you can use for learning and practicing with PostgreSQL.
  1. dvdrentalのデータベースを作成
  2. sample/dvdrental.tarを取り込む

の手順で、サンプルデータを準備します。

CREATE DATABASE dvdrental

データベースを作成し

$ pg_restore -U admin -d dvdrental /var/lib/postgresql/data/sample-db/dvdrental.tar

pg_restoreでデータを読み込み

https://www.postgresql.jp/docs/9.0/app-pgrestore.html

\c dvdrental

\cdvdrentalデータベースに移動、\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を使用します。

https://www.tableplus.io/

TablePlusを開いたらCreate a new connectionPostgreSQLを選択

  • Host 127.0.0.1
  • Port 5433
  • User admin
  • Password admin

を入力

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

サンプルファイルはこちら

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

コメント

コメントする

目次