LoginSignup
4
3

More than 3 years have passed since last update.

DockerでPostgreSQL環境を構築してSQLを実行

Posted at

はじめに

手元のPCでちょっとしたSQLの練習をしたりするときに、
DockerでPostgreSQL環境を簡単に構築してSQLを実行するまでの手順をまとめます。

本記事で使用するファイルは、全てこちらに置いてあります。
https://github.com/takumiw/postgres_practice

環境

  • macOS Big Sur
  • Docker for Mac 20.10.5

まずはDockerをインストール

Dockerをインストールしてなければ、まずはインストールから。
https://docs.docker.jp/docker-for-mac/install.html

DockerでPostgresコンテナを起動

PostgreSQLには公式のDockerイメージがあるので、今回は最新版の postgres:13.2-alpine を利用します。
https://hub.docker.com/_/postgres

以下の通りに、 Dockerfiledocker-compose.yaml を作ります。

Dockerfile
FROM postgres:13.2-alpine
ENV LANG ja_JP.utf8
docker-compose.yaml
version: '3'
services:
  db:
    build: .
    ports:
      - 5433:5432
    environment:
      POSTGRES_HOST_AUTH_METHOD: trust
      # POSTGRES_USER: admin
      # POSTGRES_PASSWORD: admin
    volumes:
      - ./:/work
    working_dir: /work

Dockerイメージをbuildして、コンテナを実行します。

$ docker-compose up -d

buildしたDockerイメージを確認

$ docker images
REPOSITORY                TAG            IMAGE ID       CREATED         SIZE
postgres_env_db           latest         fbd45d56ba79   6 days ago      160MB

実行中のコンテナを確認

$ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                    NAMES
5e677a47d11f   postgres_env_db   "docker-entrypoint.s…"   42 seconds ago   Up 41 seconds   0.0.0.0:5433->5432/tcp   postgres_env_db_1

作成したコンテナに接続

$ docker exec -it 5e677a47d11f /bin/bash

PostgreSQLで新たにデータベースを作成

データベースユーザ test を作成

# createuser -U postgres test

データベース test_db を作成

# createdb -U postgres -O test -E UTF8 --locale=C -T template0 test_db

作成したデータベースを確認

# psql -U postgres -l
                                List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | ja_JP.utf8 | ja_JP.utf8 | 
 template0 | postgres | UTF8     | ja_JP.utf8 | ja_JP.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | ja_JP.utf8 | ja_JP.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 test_db   | test     | UTF8     | C          | C          | 
(4 rows)

SQLを実行

1. データベースにアクセスしてインタラクティブに実行する場合

データベースにアクセス

# psql -U test test_db
psql (13.2)
Type "help" for help.

試しに日付を取得するSQLコマンドを実行

test_db=> select current_date;
 current_date 
--------------
 2021-04-07
(1 row)

データベースを抜ける

test_db=> \q

2. SQLコマンドを単体で実行する場合

-c オプションを使い、' ' の中に実行したいコマンドを入れる

# psql -U test test_db -c 'select current_date;'
 current_date 
--------------
 2021-04-07
(1 row)

3. SQLファイルを実行する場合

-f オプションを使い、実行したいSQLファイルのパスを指定する

# psql -U test test_db -f src/test_1.sql
CREATE TABLE
# psql -U test test_db -f src/test_2.sql
BEGIN
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
COMMIT
# psql -U test test_db -f src/test_3.sql
  id  |    name    | age 
------+------------+-----
 0001 | 山田太郎   |  28
 0002 | 佐藤達弘   |  34
 0003 | 木村幸平   |  50
 0004 | 神埼恵美   |  22
(4 rows)

参考資料

4
3
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
4
3