1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

DockerでPostgreSQLの環境構築

Last updated at Posted at 2021-07-21

動作環境

mac osx 10.15.7
Docker 20.10.5

テーブル作成までの流れ

$ docker network create db
$ docker run --name db -p 5432:5432 --network=db -v "$PWD:/var/lib/postgresql/data" -e POSTGRES_PASSWORD=password -d postgres:alpine
$ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                    NAMES
56014f6ccde1   postgres:alpine   "docker-entrypoint.s…"   15 seconds ago   Up 10 seconds   0.0.0.0:5432->5432/tcp   db
$ docker run -it --rm --network=db postgres:alpine psql -h db -U postgres
--'password'を入力
postgres=# create database student;
CREATE DATABASE
postgres=# \c student
You are now connected to database "student" as user "postgres".
student=# create table student(id BIGSERIAL PRIMARY KEY, name TEXT);
student=# \dt
          List of relations
 Schema |  Name   | Type  |  Owner
--------+---------+-------+----------
 public | student | table | postgres
(1 row)
student=# insert into student (name) values ('Alex'),('Maria');
INSERT 0 2
student=# select * from student;
 id | name
----+-------
  1 | Alex
  2 | Maria

# おまけ
使わなくなったら消しましょう

postgres=# drop database student;
DROP DATABASE
postgres=# exit
$ docker rm -f db
db
$ docker network rm db
db
1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?