Docker로 Postgresql DB를 띄우면서 테이블까지 생성하려고 init.sql 파일을 만들어 올렸는데 몇번을 해봐도 테이블 생성이 되지를 않았다. 분명히 다 제대로 한 것 같은데!
docker-compose up -d에서 -d를 빼고
docker-compose up 해서 무슨 에러라고 발생하나 하고 봤더니
postgres |
postgres | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres |
postgres | 2024-05-29 12:18:36.988 UTC [1] LOG: starting PostgreSQL 16.3 (Debian 16.3-1.pgdg120+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
postgres | 2024-05-29 12:18:36.988 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres | 2024-05-29 12:18:36.988 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres | 2024-05-29 12:18:36.992 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres | 2024-05-29 12:18:37.011 UTC [30] LOG: database system was shut down at 2024-05-29 12:18:32 UTC
postgres | 2024-05-29 12:18:37.038 UTC [1] LOG: database system is ready to accept connections
바로 맨 위에서 이런 걸 발견했다.
PostgreSQL Database directory appears to contain a database; Skipping initialization
이니셜라이제이션을 스킵한다는건가!? 그러면 내가 넣은 init.sql 파일이 당연히 실행이 안되겠구나.
아, 이미 (도커를 끈다고해서 DB에 저장되었던 데이터가 다 날아가는 걸 방지하기 위해 연결해둔) volume에 저장된 (이전에 init.sql 파일을 만들기 전에 docker를 띄울 때 이미 생긴) 데이터베이스가 있어서 시작과정을 스킵한다는 건가? 그럼 그걸 지우면 되나?
https://stackoverflow.com/questions/59715622/docker-compose-and-create-db-in-postgres-on-init
검색해봤더니 위 글이 나왔고, 그게 맞았습니다.
저는 아직 데이터가 없었기 때문에 제 docker-compose.yaml 파일(아래)에 있는
version: '3.5'
services:
db:
image: postgres
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
container_name: postgres
volumes:
- ./pgdata:/var/lib/postgresql/data
- ./docker/postgresql/init/:/docker-entrypoint-initdb.d/
ports:
- '5432:5432'
pgdata라는 제 로컬 폴더를 삭제하고 다시 docker-compose up 했더니 이번에야말로 initinalization이 실행되면서 제 의도대로 테이블을 생성할 수 있었습니다. (아래처럼 나옵니다.)
postgres | 2024-05-29 12:42:19.013 UTC [49] LOG: database system is ready to accept connections
postgres | done
postgres | server started
postgres |
postgres | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
postgres | CREATE TABLE