IT story

Docker Postgres 스크립트에서 사용자 / 데이터베이스를 만드는 방법

hot-time 2020. 5. 24. 10:51
반응형

Docker Postgres 스크립트에서 사용자 / 데이터베이스를 만드는 방법


사용자 정의 사용자 및 데이터베이스를 만들어 개발 postgres 인스턴스의 컨테이너를 설정하려고했습니다. 공식 postgres 도커 이미지를 사용하고 있습니다. 문서에서 /docker-entrypoint-initdb.d/사용자 정의 매개 변수를 사용하여 데이터베이스를 설정하기 위해 폴더 안에 bash 스크립트를 삽입하도록 지시합니다 .

내 bash 스크립트 : make_db.sh

su postgres -c "createuser -w -d -r -s docker"
su postgres -c "createdb -O docker docker"

도커 파일

FROM library/postgres

RUN ["mkdir", "/docker-entrypoint-initdb.d"]
ADD make_db.sh /docker-entrypoint-initdb.d/

docker logs -f db(db는 내 컨테이너 이름입니다) 에서 얻는 오류 는 다음과 같습니다.

createuser : 데이터베이스에 연결할 수 없습니다 postgres : 서버에 연결할 수 없습니다 : 해당 파일 또는 디렉토리가 없습니다

/docker-entrypoint-initdb.d/postgres가 시작되기 전에 폴더 내부의 명령 이 실행되고 있는 것 같습니다 . 내 질문은 공식 postgres 컨테이너를 사용하여 프로그래밍 방식으로 사용자 / 데이터베이스를 어떻게 설정합니까? 스크립트로 이것을 할 수있는 방법이 있습니까?


편집-2015 년 7 월 23 일부터

공식 포스트 그레스 고정 표시기 이미지 실행 .sql스크립트는에있는 /docker-entrypoint-initdb.d/폴더.

따라서 필요한 것은 다음 SQL 스크립트를 작성하는 것입니다.

init.sql

CREATE USER docker;
CREATE DATABASE docker;
GRANT ALL PRIVILEGES ON DATABASE docker TO docker;

Dockerfile에 추가하십시오.

도커 파일

FROM library/postgres
COPY init.sql /docker-entrypoint-initdb.d/

그러나 2015 년 7 월 8 일 이후 로 사용자와 데이터베이스를 작성 하기 만하면 POSTGRES_USER, POSTGRES_PASSWORDPOSTGRES_DB환경 변수 를보다 쉽게 ​​사용할 수 있습니다 .

docker run -e POSTGRES_USER=docker -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=docker library/postgres

또는 Dockerfile로 :

FROM library/postgres
ENV POSTGRES_USER docker
ENV POSTGRES_PASSWORD docker
ENV POSTGRES_DB docker

2015 년 7 월 23 일보다 오래된 이미지

에서 postgres의 도커 이미지의 문서 , 그것은 있다고한다

[...] /docker-entrypoint-initdb.d서비스를 시작하기 전에 추가 초기화를 수행하기 위해 해당 디렉토리 [ ] 에서 찾은 * .sh 스크립트를 소싱합니다.

여기서 중요한 것은 "서비스를 시작하기 전에" 입니다. 이는 postgres 서비스가 시작되기 전에 make_db.sh 스크립트 가 실행되므로 "postgres 데이터베이스에 연결할 수 없습니다" 라는 오류 메시지가 표시 됩니다.

그 후 또 다른 유용한 정보가 있습니다.

초기화의 일부로 SQL 명령을 실행해야하는 경우 Postgres 단일 사용자 모드를 사용하는 것이 좋습니다.

첫눈에 이것은 다소 신비로운 일이라고 동의했다. 말하는 것은 초기화 스크립트가 동작을 수행하기 전에 postgres 서비스를 단일 모드로 시작해야한다는 것입니다. 따라서 make_db.ksh 스크립트를 다음과 같이 변경할 수 있으며 원하는 것에 더 가까이 갈 수 있습니다.

참고 , 이것은 최근 다음 커밋에서 변경되었습니다 . 이것은 최신 변경 사항과 함께 작동합니다.

export PGUSER=postgres
psql <<- EOSQL
    CREATE USER docker;
    CREATE DATABASE docker;
    GRANT ALL PRIVILEGES ON DATABASE docker TO docker;
EOSQL

이전에는 --single모드를 사용해야했습니다.

gosu postgres postgres --single <<- EOSQL
    CREATE USER docker;
    CREATE DATABASE docker;
    GRANT ALL PRIVILEGES ON DATABASE docker TO docker;
EOSQL

이제 .sql 파일을 init 디렉토리에 넣을 수 있습니다.

문서에서

If you would like to do additional initialization in an image derived from this one, add one or more *.sql or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary). After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files and source any *.sh scripts found in that directory to do further initialization before starting the service.

So copying your .sql file in will work.


I add custom commands to a environment evoked in a CMD after starting services... I haven't done it with postgres, but with Oracle:

#set up var with noop command
RUN export POST_START_CMDS=":"
RUN mkdir /scripts
ADD script.sql /scripts
CMD service oracle-xe start; $POST_START_CMDS; tail -f /var/log/dmesg

and start with

docker run -d ... -e POST_START_CMDS="su - oracle -c 'sqlplus @/scripts/script' " <image>

.


You need to have the database running before you create the users. For this you need multiple processes. You can either start postgres in a subshell (&) in the shell script, or use a tool like supervisord to run postgres and then run any initialization scripts.

A guide to supervisord and docker https://docs.docker.com/articles/using_supervisord/


You can use this commands:

docker exec -it yournamecontainer psql -U postgres -c "CREATE DATABASE mydatabase ENCODING 'LATIN1' TEMPLATE template0 LC_COLLATE 'C' LC_CTYPE 'C';"

docker exec -it yournamecontainer psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE postgres TO postgres;"

참고URL : https://stackoverflow.com/questions/26598738/how-to-create-user-database-in-script-for-docker-postgres

반응형