TL;DR It is an article that let's make PostgreSQL easily with Docker. Since it is a training article, tools etc. are decided, but that area is Donmai.
This time the SQL client uses DBeaver. Just get the latest version for the time being. https://dbeaver.io/
Let's launch DB quickly while downloading.
Docker Desktop for Windows Without this, we can't talk. Let's install it first. https://docs.docker.jp/docker-for-windows/install.html
Hit `docker ps`
in PowerShell and you're ready to go:
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Since it is a training, from how to carefully search for an image.
Below, the command is after ```>
.
> docker search postgres
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
postgres The PostgreSQL object-relational database sy… 8763 [OK]
I found it quickly when I searched. Basically, it is better to take the image of OFFICIAL (official).
Here is the following command.
docker run -d --rm --name db -p 5555:5432 -e POSTGRES_HOST_AUTH_METHOD=trust postgres
Download the image and start it. The explanation of the command will be described later. If the container is started with `` `docker ps```, it is complete.
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9676081a5f72 postgres "docker-entrypoint.s…" 46 seconds ago Up 46 seconds 0.0.0.0:5555->5432/tcp db
Let's install and launch it.
Port at 5555.
Did you connect? Let's make a table or column.
This time, I made a "test table" as follows.
docker exec -it db bash
Connect to the container with.
> docker exec -it db bash
root@9676081a5f72:/#
root@9676081a5f72:/# psql -U postgres
psql (13.0 (Debian 13.0-1.pgdg100+1))
Type "help" for help.
postgres=# \db
List of tablespaces
Name | Owner | Location
------------+----------+----------
pg_default | postgres |
pg_global | postgres |
(2 rows)
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+----------------+-------+----------
public |Test table| table | postgres
(1 row)
postgres=#
It's done properly. Exit with \ q
and escape from the container with` `` exit```.
> docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9676081a5f72 postgres "docker-entrypoint.s…" 20 minutes ago Up 20 minutes 0.0.0.0:5555->5432/tcp db
It exists like this now, but let's stop this container.
docker stop db
It is.
Is it disappearing properly?
> docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
DB started up quickly with this one line. Seriously Docker-san, God?
docker run -d --rm --name db -p 5555:5432 -e POSTGRES_HOST_AUTH_METHOD=trust postgres
Overview | Remarks | |
---|---|---|
run | Start-up | https://docs.docker.jp/engine/reference/run.html |
-d | Detached | In short background |
--rm | Delete container when exiting container | You can delete it yourself with docker rm db which is not required |
--name db | Container name | This timedbAnd said. Optional |
-p 5555:5432 | Port forwarding | Container 5432 to host 5555 |
-e POSTGRES_HOST_AUTH_METHOD=trust | No password | If you want to give a password**-e POSTGRES_PASSWORD=password**Let's use |
postgres | Image name | The guy I searched for |
Folder structure of docker-compose and sample of Dockerfile and docker-compose.yml. This sample should respond on host 5434. Add ** app ** to ** services ** and connect to DB immediately from app via db-net. The image is that init.sql contains SQL to create DB and tables. I made it a long time ago, so please forgive me if it's old or not cool.
./
- docker-compose.yml
+ database/
- Dockerfile
+ postgres-init/
- init.sql
+ postgres/
FROM postgres:9.6.16
RUN localedef -i ja_JP -c -f UTF-8 -A /usr/share/locale/locale.alias ja_JP.UTF-8
ENV LANG ja_JP.utf8
docker-compose.yml
version: "3.5"
services:
db:
build:
context: database
volumes:
- ./postgres:/var/lib/postgresql
- ./postgres-init:/docker-entrypoint-initdb.d
expose:
- '5432'
ports:
- "5434:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_INITDB_ARGS: "--encoding=UTF-8"
restart: 'always'
networks:
- db-net
networks:
db-net:
driver: bridge
Recommended Posts