Postgresql setup with Docker

hansung.dev·2021년 4월 3일
1
post-thumbnail

Docker-Compose를 사용하여 Postgresql를 구축합니다.

Getting Started

실습 환경 정보는 아래와 같습니다.

Docker : 20.10.2, Docker Compose : 1.27.4, build 40524192
OS : macOS Big Sur Ver 11.2.2
PostgreSQL : PostgreSQL 9.6.21 (Debian)

docker-compose.yml의 docker image의 버전을 10.16, 11.11, 12.6, 13.2로 변경하면 다양한 Postgrsql Docker 환경을 구성할수 있습니다.

docker-compose 디렉토리 구조 및 파일을 살펴보도록 하겠습니다.

File Directory with Docker-Compose

postgresql-9.6.21
├── docker-compose.yml
├── postgresql-01
│   ├── conf
│   │   └── postgresql.conf
│   ├── data
└───└── log

"docker-compose.yml", "postgresql.cnf"를 살펴보도록 하겠습니다.

docker-compose.yml

version: '3.7'

services:
  postgresql-01:
    container_name: postgresql-01
    hostname: postgresql-01
    image: postgres:9.6.21
    mem_limit: "1g"
    command: "postgres -c config_file=/etc/postgresql/postgresql.conf"
    restart: always
    volumes:
      - "${PWD}/postgresql-01/data:/var/lib/postgresql/data"
      - "${PWD}/postgresql-01/log:/var/log/postgresql"
      - "${PWD}/postgresql-01/conf/postgresql.conf:/etc/postgresql/postgresql.conf"
    ports:
      - 5432:5432
    environment:
      POSTGRES_PASSWORD: "root"
      TZ: "Asia/Seoul"
    networks:
      - mybridge
networks:
  mybridge:
    external: true

다른 버전의 Postgresql의 구축이 필요한 경우 docker-compose.yml의 image 구문을 아래와 같이 변경하시면 됩니다. (ex. image: postgres:10.16, image: postgres:12.6, image: postgres:13.2)

postgresql.conf

# -----------------------------
# PostgreSQL configuration file
# -----------------------------

#------------------------------------------------------------------------------
# FILE LOCATIONS
#------------------------------------------------------------------------------

#data_directory = 'ConfigDir'		# use data in another directory
					# (change requires restart)

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -
listen_addresses = '*'			# (change requires restart)
#port = 5432				# (change requires restart)
max_connections = 100			# (change requires restart)

#------------------------------------------------------------------------------
# RESOURCE USAGE (except WAL)
#------------------------------------------------------------------------------

# - Memory -
shared_buffers = 128MB			# min 128kB
					# (change requires restart)
# - Disk -
# - Kernel Resource Usage -
# - Cost-Based Vacuum Delay -
# - Background Writer -
# - Asynchronous Behavior -

#------------------------------------------------------------------------------
# WRITE AHEAD LOG
#------------------------------------------------------------------------------

# - Settings -
# - Checkpoints -
# - Archiving -

#------------------------------------------------------------------------------
# REPLICATION
#------------------------------------------------------------------------------

# - Sending Server(s) -
# - Master Server -
# - Standby Servers -

#------------------------------------------------------------------------------
# QUERY TUNING
#------------------------------------------------------------------------------

# - Planner Method Configuration -
# - Planner Cost Constants -
# - Genetic Query Optimizer -
# - Other Planner Options -

#------------------------------------------------------------------------------
# ERROR REPORTING AND LOGGING
#------------------------------------------------------------------------------

# - Where to Log -
log_directory = '/var/log/postgresql'
log_filename = 'postgresql-%Y_%m_%d_%H%M%S.log'

# - When to Log -
# - What to Log -
log_timezone = 'Asia/Seoul'
# - Process Title -

#------------------------------------------------------------------------------
# RUNTIME STATISTICS
#------------------------------------------------------------------------------

# - Query/Index Statistics Collector -
# - Statistics Monitoring -

#------------------------------------------------------------------------------
# AUTOVACUUM PARAMETERS
#------------------------------------------------------------------------------

#autovacuum = on			# Enable autovacuum subprocess?  'on'

#------------------------------------------------------------------------------
# CLIENT CONNECTION DEFAULTS
#------------------------------------------------------------------------------

# - Statement Behavior -
# - Locale and Formatting -
datestyle = 'iso, mdy'
timezone = 'Asia/Seoul'

# These settings are initialized by initdb, but they can be changed.
lc_messages = 'en_US.utf8'			# locale for system error message
					# strings
lc_monetary = 'en_US.utf8'			# locale for monetary formatting
lc_numeric = 'en_US.utf8'			# locale for number formatting
lc_time = 'en_US.utf8'				# locale for time formatting

# default configuration for text search
default_text_search_config = 'pg_catalog.english'

# - Other Defaults -

#------------------------------------------------------------------------------
# LOCK MANAGEMENT
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
# VERSION/PLATFORM COMPATIBILITY
#------------------------------------------------------------------------------

# - Previous PostgreSQL Versions -
# - Other Platforms and Clients -

#------------------------------------------------------------------------------
# ERROR HANDLING
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
# CONFIG FILE INCLUDES
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
# CUSTOMIZED OPTIONS
#------------------------------------------------------------------------------
# Add settings for extensions here

Setting up and running

Docker-Compos Run, Stop

docker-compose를 실행하여 Elastic Stack 및 Mysql 서비스를 시작합니다.

# run
docker-compose up -d

#stop
docker-compose stop

Connecting to Postgresql

DBeaver 또는 PGAdmin를 다운 받고 연결정보를 입력 후 접속합니다. 여기서는 DBeaver로 접속해보겠습니다.

ip : localhost
port : 5432
user : postgres
password : root

postgresql 접속 후 "select version()" 명령어로 postgresql 버전을 확인 합니다. postgresql 버전이 "9.6.21"으로 표시된 부분을 확인할수 있습니다.

profile
Data Engineer

0개의 댓글