0
votes

When start a docker container from postgres image, it only creates a public schema under a database. here is my setting

postgres:
        container_name: postgres
        image: postgres:latest
        ports:
            - "${DB_PORT}:5432"
        environment:
            - POSTGRES_USER=${DB_USER}
            - POSTGRES_PASSWORD=${DB_PASSWORD}
            - POSTGRES_DB=${DB_NAME}

my solution is

// 111111-create-schema.js
await queryInterface.createSchema('custom');

// 222222-create-user.js
await queryInterface.createTable('Users', {}, {schema: 'custom'}

When I run npx sequelize-cli db:migrate, I do see my user table in custom schema. However, the sequelizeMeta table which stores the migrations is in PUBLIC shema

I also tried to add schema: 'custom' in config.json. however, I got custom schema is not found error when I run db:migrate. I assume at that point my migration script has not run yet before it trys to locate this custom schema.

Then I tried to manually create the schema in the pgadmin. then I see both my user and sequelizeMeta table are in this custom schema.

I am wondering how I can let postgres docker container precreate a schema just like it precreate a database using a POSTGRES_DB environment variable ? something like

postgres:
        container_name: postgres
        image: postgres:latest
        ports:
            - "${DB_PORT}:5432"
        environment:
            - POSTGRES_USER=${DB_USER}
            - POSTGRES_PASSWORD=${DB_PASSWORD}
            - POSTGRES_DB=${DB_NAME}
            - POSTGRES_SCHEMA=${DB_SCHEMA} <---------------

Or there is an easy or proper way in sequelize migration that I missed ?

1
You could create a script that can be executed as a command to the container when the container launches and that script can pre create the schema in postgres.Aayush Mall

1 Answers

0
votes

When you create your sequelize connection say your schema on define object like this:

const sequelize = new Sequelize(database, username, password,
{
   host, dialect, port, omitNull: true,
   define: {
     schema: "your-schema"
     timestamps: true,
     underscored: true
   },
   ...
 })