Database/PostgreSQL
PostgreSQL Docker 설치
기록하는 백앤드개발자
2024. 4. 18. 10:05
반응형
ㅁ 들어가며
ㅇ 지난 글에서 MySQL과 PostgreSQL의 차이점에 대해서 알아보았다.
ㅇ 이번 글은 PostgreSQL을 테스트 하기 위해 Docker 환경에 구축하는 과정을 정리하였다.
ㅇ docker cli와 docker-compose 두가지 방법을 정리하였다.
ㅁ docker run
docker run -d \
-p 5432:5432 \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=strongpwd \
-e POSTGRES_DB=postgres \
-v /Volumes/500GB/data/postgres:/var/lib/postgresql/data \
--name postgres \
postgres:latest
# 기동확인
$ docker logs postgres
~~~~~~
2024-04-16 12:25:06.664 UTC [1] LOG: starting PostgreSQL 16.2 (Debian 16.2-1.pgdg120+2) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
2024-04-16 12:25:06.665 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2024-04-16 12:25:06.665 UTC [1] LOG: listening on IPv6 address "::", port 5432
2024-04-16 12:25:06.670 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2024-04-16 12:25:06.690 UTC [62] LOG: database system was shut down at 2024-04-16 12:25:06 UTC
2024-04-16 12:25:06.723 UTC [1] LOG: database system is ready to accept connections
ㅇ 이번엔 Docker-compose로 구현하기 위해 컨테이너를 정지하고 삭제하였다.
# postgres 정지
$ docker stop postgres
# container 삭제
$ docker rm postgres
ㅁ Docker-compose
services:
postgres:
image: postgres:latest
restart: always
ports:
- "5432:5432"
volumes:
- /Volumes/500GB/data/postgres:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: strongpwd
POSTGRES_DB: postgres
ㅇ docker-compose.yml 작성
# 실행
$ docker-compose up
ㅁ DataGrip 접속 테스트
ㅁ 데이터베이스 생성 및 데이터 생성
-- 데이터베이스 생성
create database test_db;
-- 테이블 생성
create table test
(
ID int primary key,
Name varchar(40) not null,
AGE int not null
);
-- 첫 데이터 생성
insert into test(ID, Name, AGE) values(1,'peterica', 20);
-- 조회
select * from test;
ㅁ 함께 보면 좋은 사이트
반응형