How to specify the current working database for the initialization script of a postgres docker container?

后端 未结 1 936
礼貌的吻别
礼貌的吻别 2021-01-16 07:17

It is known that one can copy an init.sql file to be executed on the creation of the container with a docker command similar to this one: COPY init.sql /docker-entrypo

相关标签:
1条回答
  • 2021-01-16 07:36

    This explains in detail the initialization of the database. How to create User/Database in script for Docker Postgres.

    To put it briefly, the name of the database created during the initialization and where your tables get created by default, is given by the environment variable POSTGRES_DB. If the variable is not set the default value postgres is used instead.

    The scripts in the docker-entrypoint-initdb.d folder are executed one by one with the following command:

    psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -f <your-file>
    

    therefore you are connected to the POSTGRES_DB database (have a look at the docker-entrypoint.sh script).

    In your script files you can nevertheless connect to a different database using the meta-command

    \connect DBNAME
    
    0 讨论(0)
提交回复
热议问题