Skip to content

npx sequelize-cli db:migrate command don't read the environment variables with PM2 #1514

Open
@aobregonmx

Description

@aobregonmx

What you are doing?

npx sequelize-cli db:migrate command don't read the environment variables

This command works without problems in my local environment on macOS Sonoma 14.2.1 but when I try to run it in a development environment (PM2 version 5.3.1) it does not work

.sequelizerc

const path = require('path');

module.exports = {
    'config': path.resolve('src/db/config', 'config.cjs'),
    'models-path': '../src/db/models/',
    'migrations-path': path.resolve('src/db', 'migrations'),
    'seeders-path': path.resolve('src/db', 'seeders'),
}

config.cjs

const config = require('../../common/config.js');

module.exports = {
  development: {
    username: config.dbUser,
    password: config.dbPassword,
    database: config.dbName,
    host: config.dbHost,
    dialect: 'postgres',
    migrationStorageTableSchema: 'vic',
    logging: !config.isProd,
  },
  test: {
    username: 'root',
    password: null,
    database: 'database_test',
    host: '127.0.0.1',
    dialect: 'postgres',
  },
  production: {
    username: 'root',
    password: null,
    database: 'database_production',
    host: '127.0.0.1',
    dialect: 'postgres',
  },
};

common/config.js

require('dotenv').config();

const config = {
  env: process.env.NODE_ENV || 'dev',
  host: process.env.HOST || 'localhost',
  port: process.env.PORT || 3000,
  dbHost: process.env.DB_HOST,
  dbPort: process.env.DB_PORT,
  dbName: process.env.DB_NAME,
  dbUser: process.env.DB_USER,
  dbPassword: process.env.DB_PASSWORD,
  isProd: process.env.NODE_ENV === 'production',
};

module.exports = config;

What do you expect to happen?

Get a message similar to the one obtained in my local macOS environment

> [email protected] migrations:run
> sequelize-cli db:migrate


Sequelize CLI [Node: 20.11.0, CLI: 6.6.2, ORM: 6.37.3]

Loaded configuration file "src/db/config/config.cjs".
Using environment "development".
Executing (default): SELECT 1+1 AS result
Executing (default): CREATE SCHEMA IF NOT EXISTS "vic";
Executing (default): SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type LIKE '%TABLE' AND table_name != 'spatial_ref_sys';
Executing (default): SELECT pk.constraint_type as "Constraint",c.column_name as "Field", c.column_default as "Default",c.is_nullable as "Null", (CASE WHEN c.udt_name = 'hstore' THEN c.udt_name ELSE c.data_type END) || (CASE WHEN c.character_maximum_length IS NOT NULL THEN '(' || c.character_maximum_length || ')' ELSE '' END) as "Type", (SELECT array_agg(e.enumlabel) FROM pg_catalog.pg_type t JOIN pg_catalog.pg_enum e ON t.oid=e.enumtypid WHERE t.typname=c.udt_name) AS "special", (SELECT pgd.description FROM pg_catalog.pg_statio_all_tables AS st INNER JOIN pg_catalog.pg_description pgd on (pgd.objoid=st.relid) WHERE c.ordinal_position=pgd.objsubid AND c.table_name=st.relname) AS "Comment" FROM information_schema.columns c LEFT JOIN (SELECT tc.table_schema, tc.table_name, cu.column_name, tc.constraint_type FROM information_schema.TABLE_CONSTRAINTS tc JOIN information_schema.KEY_COLUMN_USAGE  cu ON tc.table_schema=cu.table_schema and tc.table_name=cu.table_name and tc.constraint_name=cu.constraint_name and tc.constraint_type='PRIMARY KEY') pk ON pk.table_schema=c.table_schema AND pk.table_name=c.table_name AND pk.column_name=c.column_name WHERE c.table_name = 'SequelizeMeta' AND c.table_schema = 'public'
Executing (default): SELECT table_name FROM information_schema.tables WHERE table_schema = 'vic' AND table_name = 'SequelizeMeta'
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a, pg_namespace s WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'SequelizeMeta' AND s.oid = t.relnamespace AND s.nspname = 'vic' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
Executing (default): SELECT "name" FROM "vic"."SequelizeMeta" AS "SequelizeMeta" ORDER BY "SequelizeMeta"."name" ASC;

What is actually happening?

I am getting this message in the terminal, where it is not reading the environment variables and is not creating the models in the database

Sequelize CLI [Node: 20.11.0, CLI: 6.6.2, ORM: 6.37.3]

Loaded configuration file "src/db/config/config.cjs".
Using environment "development".

ERROR: connect ECONNREFUSED 127.0.0.1:5432

I tried printing the variables and either way I'm getting undefined

Dialect: postgres
Database version: PostgreSQL 12.19 (Ubuntu 12.19-0ubuntu0.20.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.3) 9.4.0, 64-bit
Sequelize CLI version: 6.6.2
Sequelize version: 6.37.3
PM2 version: 5.3.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions