LoginSignup
66
45

More than 1 year has passed since last update.

docker上で動かしているpostgresに接続する

Last updated at Posted at 2018-06-02

起動 (docker-compose)

docker-compose ファイルを作成:

docker-compose.postgres.yml
version: '3'

services:
  db:
    container_name: postgres
    image: postgres:14
    volumes:
      - ./docker/pg:/var/lib/postgresql
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: test_db
    ports:
      - '5433:5432'

portsは、<つなぐport>:<dockerのContainerでのport>順番を間違えないようにする。ここで、設定した、User,Password,Portを使って後ほどつなぐ

起動:

docker-compose -f docker-compose.postgres.yml up -d

チェック:

docker-compose -f docker-compose.postgres.yml ps
  Name                Command              State           Ports
-------------------------------------------------------------------------
postgres   docker-entrypoint.sh postgres   Up      0.0.0.0:5433->5432/tcp

接続

Terminalから接続 (psqlコマンドがインストールされている場合)

psql -h 127.0.0.1 -p 5433 -U postgres test_db
Password for user postgres:
psql (14.3, server 14.4 (Debian 14.4-1.pgdg110+1))
Type "help" for help.

test_db=#

Dockerコンテナないのpsqlから接続

docker exec -it postgres psql -U postgres test_db
psql (14.4 (Debian 14.4-1.pgdg110+1))
Type "help" for help.

test_db=#

PSequel(GUI)から接続 (動かない可能性あり)

https://www.psequel.com/ (mac 用)

ここからダウンロードして、インストール

PSequel設定

image

データベース操作基本

ローカルのsqlファイルからSchema作成

test_table.sql
CREATE TABLE IF NOT EXISTS test_table (
	id serial PRIMARY KEY,
	name VARCHAR ( 50 ) UNIQUE NOT NULL,
	created_on TIMESTAMP NOT NULL
);

docker execで test_table.sqlをtest_dbに流す:

docker exec -i postgres psql -U postgres test_db < test_table.sql

docker execでテーブルの確認

docker exec -i postgres psql -U postgres test_db -c '\dt'
           List of relations
 Schema |    Name    | Type  |  Owner
--------+------------+-------+----------
 public | test_table | table | postgres
(1 row)

カラムの確認

docker exec -i postgres psql -U postgres test_db -c "SELECT column_name, column_default, is_nullable, data_type FROM information_schema.columns WHERE TABLE_NAME = 'test_table';"
 column_name |             column_default             | is_nullable |          data_type
-------------+----------------------------------------+-------------+-----------------------------
 id          | nextval('test_table_id_seq'::regclass) | NO          | integer
 created_on  |                                        | NO          | timestamp without time zone
 name        |                                        | NO          | character varying
(3 rows)

テストデータをcsvから入れる

csvファイルの準備

test_data.csv
id,name,created_on
1,first,2022-07-14 01:12:00

copyコマンドでinsert (copyコマンドに関してはPostgresのデータをファイルへ書き出す (psqlでCopyコマンド)も)

docker exec -i postgres psql -U postgres test_db -c "\copy test_table from STDIN with DELIMITER ',' CSV header" < test_data.csv
COPY 1

確認:

docker exec -i postgres psql -U postgres test_db -c "select * from test_table"

結果:

 id | name  |     created_on
----+-------+---------------------
  1 | first | 2022-07-14 01:12:00
(1 row)

参考

66
45
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
66
45