Creating a table in single user mode in postgres

前端 未结 3 1759
星月不相逢
星月不相逢 2021-01-12 09:12

Im trying to create a Dockerfile from the postgres image. The repo says that initialization should be handled by placing a shell script in /docker-entrypoint-initdb.d/. I pu

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-12 09:59

    I tested your script and it is almost working fine. Using Postgresql 9.4 I managed to make the following to work:

    gosu postgres postgres --single -jE <<- EOSQL
       CREATE DATABASE orpheus;
    EOSQL
    echo    
    gosu postgres postgres --single -jE orpheus <<- EOSQL
       CREATE USER docker WITH ENCRYPTED PASSWORD 'pwd_docker';
       GRANT ALL PRIVILEGES ON DATABASE orpheus to docker;
       CREATE TABLE profiles (
         profile_id    SERIAL UNIQUE PRIMARY KEY,
         user_id integer NOT NULL UNIQUE,
         profile_photo_id integer NOT NULL UNIQUE,
         age integer
       );
       CREATE TABLE hidden_user (
         owner_id    integer NOT NULL PRIMARY KEY,
         target_id integer NOT NULL
       );
    EOSQL
    echo
    

    Basically I had to split the script in two as the postgresql was complaining that the create database couldn't be used in multiline scripts. And the other was only to add the database name, orpheus, on the second command.

    And voilà

提交回复
热议问题