LoginSignup
1
1

More than 3 years have passed since last update.

手っ取り早くPostgreSQLのサンプルDBに接続確認してみる手順

Last updated at Posted at 2020-08-19

Docker使って、こちらのサンプルDBをDockerのPostgreSQLに投入する手順です。

PostgreSQLそのものの機能というより、別サービスとの接続確認などで使います。

準備

# ディレクトリ作成 && 移動
mkdir pg-sample && cd pg-sample

# サンプルDBのダウンロード
wget https://sp.postgresqltutorial.com/wp-content/uploads/2019/05/dvdrental.zip && unzip dvdrental.zip

コンテナ起動

PASSWORD=passw0rd
VERSION=12.4
docker run --rm -d --name sampledb \
  -p 5432:5432 \
  -e POSTGRES_PASSWORD=${PASSWORD} \
  -v $(pwd):/tmp/data \
  postgres:${VERSION}-alpine

サンプルDBのデータ投入

docker exec sampledb psql -U postgres -c "create database dvdrental"
docker exec sampledb pg_restore -U postgres -d dvdrental /tmp/data/dvdrental.tar

使ってみる

psql で繋いでみる。

$ docker exec -it sampledb psql -U postgres
psql (12.4 (Debian 12.4-1.pgdg100+1))
Type "help" for help.

postgres=# \c dvdrental
You are now connected to database "dvdrental" as user "postgres".
dvdrental=# \d
                     List of relations
 Schema |            Name            |   Type   |  Owner
--------+----------------------------+----------+----------
 public | actor                      | table    | postgres
 public | actor_actor_id_seq         | sequence | postgres
 public | actor_info                 | view     | postgres
 public | address                    | table    | postgres
 public | address_address_id_seq     | sequence | postgres
 public | category                   | table    | postgres

5432ポートで繋ぐことも可能です。

$ docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                    NAMES
02857907c9a9        postgres:12.4-alpine   "docker-entrypoint.s…"   7 minutes ago       Up 7 minutes        0.0.0.0:5432->5432/tcp   sampledb

片付け

docker rm -f sampledb
1
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
1
1