본문 바로가기
MLOps/Docker

[Docker] PostgreSQL 서버 생성

by Toritol 2023. 12. 12.
728x90

 

이번 글에서는 Docker에서 PostgreSQL 서버를 생성하는 방법에 대해 살펴보고, 과정에서 필요한 docker 명령어에 대해서도 설명하고자 합니다. 해당 과정은 https://mlops-for-mle.github.io/tutorial/ 사이트의 실습 과정을 학습하며 작성하였음을 미리 알려드립니다. 

 

우선 PostgreSQL 서버는 docker run 명령어를 통해 생성하는 것이 가능합니다. 명령어 뒤에 여러 옵션들이 붙는데, 하나씩 살펴보겠습니다.

 

  • -d : container를 detached 모드로 실행하는 옵션으로, 해당 이미지가 백그라운드로 실행
  • --name : container의 이름 지정
  • -p : container에서 외부로 노출할 port forwarding을 설정하는 옵션으로, 형식은 host:contaienr
  • -e : 환경 변수 설정 옵션
  • postgres:14.0 : pull하고자 하는 image와 version
$ docker run -d \
  --name postgres-server \
  -p 5432:5432 \
  -e POSTGRES_USER=myuser \
  -e POSTGRES_PASSWORD=mypassword \
  -e POSTGRES_DB=mydatabase \
  postgres:14.0

Unable to find image 'postgres:14.0' locally
14.0: Pulling from library/postgres
7d63c13d9b9b: Pull complete
cad0f9d5f5fe: Pull complete
ff74a7a559cb: Pull complete
c43dfd845683: Pull complete
e554331369f5: Pull complete
d25d54a3ac3a: Pull complete
bbc6df00588c: Pull complete
d4deb2e86480: Pull complete
cb59c7cc00aa: Pull complete
80c65de48730: Pull complete
1525521889be: Pull complete
38df9e245e81: Pull complete
79300c1d4f7a: Pull complete
Digest: sha256:db927beee892dd02fbe963559f29a7867708747934812a80f83bff406a0d54fd
Status: Downloaded newer image for postgres:14.0
001a4d0fc9d620e2a3ffaa8dad33667ff664563c575e59ff9f232017e1ac6bd2

 

docker ps 명령어는 container 확인 명령어로, 아래처럼 PostgreSQL 서버가 잘 생성된 것을 확인할 수 있습니다.

$ docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS                    NAMES
001a4d0fc9d6   postgres:14.0   "docker-entrypoint.s…"   25 seconds ago   Up 23 seconds   0.0.0.0:5432->5432/tcp   postgres-server

 

PostgreSQL 서버에 접속하여 정상적으로 작동하는지 확인하기 위해서는 psql을 설치해야 합니다. psql의 설치는 해당 사이트에서 설치가 가능하며, 저는 Windows에서 WSL로 Ubuntu를 사용하고있기 때문에, Linux에 해당하는 방법으로 설치하였습니다. 총 4개의 명령어가 사용되며, 과정은 다음과 같습니다.

$ sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
OK

$ sudo apt-get update
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Hit:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
Get:5 https://apt.postgresql.org/pub/repos/apt jammy-pgdg InRelease [123 kB]
Get:6 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [1244 kB]
Get:7 https://apt.postgresql.org/pub/repos/apt jammy-pgdg/main amd64 Packages [296 kB]
Get:8 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1018 kB]
Fetched 2910 kB in 3s (876 kB/s)
Reading package lists... Done
W: https://apt.postgresql.org/pub/repos/apt/dists/jammy-pgdg/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.

$ sudo apt-get -y install postgresql
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
(생략)...
Setting up postgresql (16+256.pgdg22.04+1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.5) ...
/sbin/ldconfig.real: /usr/lib/wsl/lib/libcuda.so.1 is not a symbolic link

 

psql의 설치가 완료되었다면, 아래 명령어를 통해 PostgreSQL 서버에 접속하는 것이 가능합니다. 각 옵션들은 앞서 docker run 명령어를 사용할 때 지정했던 값들입니다. 접속한 이후 select 1; 명령어를 날려보면, 결과가 잘 출력되며 PostgreSQL 서버가 정상적으로 작동하는 것을 확인할 수 있습니다.

$ PGPASSWORD=mypassword psql -h localhost -p 5432 -U myuser -d mydatabase

psql (16.1 (Ubuntu 16.1-1.pgdg22.04+1), server 14.0 (Debian 14.0-1.pgdg110+1))
Type "help" for help.

mydatabase=# select 1;
 ?column?
----------
        1
(1 row)

 

 

<참고>

https://mlops-for-mle.github.io/tutorial/

 

MLOps for MLE | ML Engineer를 위한 MLOps

Description will go into a meta tag in <head />

mlops-for-mle.github.io

 

 

 

728x90

댓글