I am trying to perform my migrations on the database provided by Heroku, but the following error appears:
Error during migration run:
Error: self signed certificate
at TLSSocket.onConnectSecure (_tls_wrap.js:1501:34)
at TLSSocket.emit (events.js:315:20)
at TLSSocket._finishInit (_tls_wrap.js:936:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:710:12) {
code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
}
ormconfig.js
module.exports = {
type: `${process.env.DATABASE_TYPE}`,
host: `${process.env.DATABASE_HOST}`,
port: `${process.env.DATABASE_PORT}`,
username: `${process.env.DATABASE_USERNAME}`,
password: `${process.env.DATABASE_PASSWORD}`,
database: `${process.env.DATABASE_NAME}`,
entities: [`${process.env.DATABASE_ENTITIES}`],
migrations: [`${process.env.DATABASE_MIGRATIONS}`],
cli: { migrationsDir: `${process.env.DATABASE_MIGRATIONS_DIR}` },
extra: {
ssl: true
}
};
module.exports = {
type: `postgres`,
url: `${process.env.DATABASE_URL}`,
entities: [`${process.env.DATABASE_ENTITIES}`],
migrations: [`${process.env.DATABASE_MIGRATIONS}`],
cli: { migrationsDir: `${process.env.DATABASE_MIGRATIONS_DIR}` },
extra: {
ssl: {
rejectUnauthorized: false,
}
}
};
Works fine!
Related
Using latest version of TypeORM: 0.3.9 with NestJS.
While I'm trying to generate migration with following command:
npm run typeorm migration:generate -- ./migrations/Init -o -d ormconfig.js
Getting error:
Error during migration generation:
Error: Given data source file must contain export of a DataSource instance
Here is my DataSource file:
ormconfig.js
var dbConfig = {
synchronize: false,
migrations: ['migrations/*.js'],
cli: {
migrationsDir: 'migrations',
},
};
switch (process.env.NODE_ENV) {
case 'development':
Object.assign(dbConfig, {
type: 'sqlite',
database: 'db.sqlite',
entities: ['**/*.entity.js'],
});
break;
case 'test':
Object.assign(dbConfig, {
type: 'sqlite',
database: 'test.sqlite',
entities: ['**/*.entity.ts'],
migrationsRun: true,
});
break;
case 'production':
Object.assign(dbConfig, {
type: 'postgres',
url: process.env.DATABASE_URL,
migrationsRun: true,
entities: ['**/*.entity.js'],
ssl: {
rejectUnauthorized: false,
},
});
break;
default:
throw new Error('unknown environment');
}
module.exports = dbConfig;

Also TypeORM script snippet from:
package.json
"scripts": {
"typeorm": "cross-env NODE_ENV=development node --require ts-node/register ./node_modules/typeorm/cli.js"
},
I got this error while trying to run Typeorm migration:
AlreadyHasActiveConnectionError: Cannot create a new connection named "default", because connection with such name already exist and it now has an active connection session.
It works on one machine, but doesn't work on another and on Heroku. Maybe there is a specific command to kill all existing connections?
keepConnectionAlive didn't help.
Here is my ormconfig file:
module.exports = {
type: "postgres",
host: process.env.DB_HOST,
port: +process.env.DB_PORT,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
entities: ["dist/**/*.entity.js"],
synchronize: true,
autoLoadEntities: true,
migrations: ["dist/migration/**/*.js"],
cli: {
migrationsDir: "src/migration",
},
ssl: process.env.DB_ENV === 'development' ? false : {
rejectUnauthorized: false
}
};
Connecting to my my DigitalOcean database with Sequelize works fine when I'm not migrating. For example, attempting to create a new table works just fine; the code below successfully connects and creates a new table.
sequelize = new Sequelize(config.use_env_variable, config);
sequelize.authenticate().then(console.log('success')).catch((error) => console.log(error));
sequelize.define('test-table', {
test_id: {
type: Sequelize.INTEGER,
},
});
sequelize.sync();
I have a CA certificate .crt file I downloaded from DigitalOcean that I'm passing in with the Sequelize options. My config.js looks like
development: {
use_env_variable: 'postgresql://[digitalocean_host_url]?sslmode=require',
ssl: true,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
ca: fs.readFileSync(`${__dirname}/../.postgresql/root.crt`),
},
},
},
However when I try to create tables using migrations with
npx sequelize-cli db:migrate
I receive the following output and error:
Parsed url postgresql://[digitalocean_host_url]?sslmode=require
ERROR: no pg_hba.conf entry for host [host], user [user], database [database], SSL off
Which is very strange, because SSL is working when I create a table using just Sequelize sync. I have a .sequelizerc file for the sequelize-cli configurations, which looks like this:
const path = require('path');
const env = process.env.NODE_ENV || 'development'
const config = require('./config/config')[env];
module.exports = {
'config': path.resolve('config', 'config.js'),
'url': config.use_env_variable,
'options-path': path.resolve('config', 'sql-options.json')
}
inside my sql-options.json I have the following
{
"use_env_variable": "postgresql://[digitalocean_host_url]?sslmode=require",
"dialect":"postgres",
"ssl": true,
"dialectOptions": {
"ssl": {
"required": true,
"rejectUnauthorized": true,
"ca": "/../.postgresql/root.crt"
}
}
}
I've tried a lot of the advice from various resources, including the sequelize/cli repo. But none of it seems to work. Any advice would be helpful.
I had the same issue and the fix was to add the code below in the migrations config file even though you already have it in the database connection file.
The following code is in the config/config.js file for migrations.
production: {
username: ****,
password: ****,
database: ****,
host: ****,
dialect: ****,
port: ****,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
},
},
},
This is how my DB connection looks like that was working normally.
const sequelize = new Sequelize({
host: ****,
database: ****,
username: ****,
password: ****,
dialect: ****,
port: ****,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
},
},
});
I'm following a tutorial on youtube that uses node express knex and sqlite locally and postgres on heroku. I managed to get everything working locally with sqlite and have managed to load the app on heroku. I get the initial home message. I manged to create a blank postgres database on heroku and can look at the database credentials on heroku. I need to migrate my knex tables to the heroku postgres database. According to the video below I need to use this instruction
heroku run knex migrate:latest -a node-knex1
Which gives me the following error
Running knex migrate:latest on ⬢ node-knex1... up, run.3492 (Free)
Using environment: production
error: no pg_hba.conf entry for host "54.76.162.141", user "vnujkqszmxsboi", database "def52ulvb1tjg9", SSL off
at Parser.parseErrorMessage (/app/node_modules/pg-protocol/dist/parser.js:278:15)
at Parser.handlePacket (/app/node_modules/pg-protocol/dist/parser.js:126:29)
at Parser.parse (/app/node_modules/pg-protocol/dist/parser.js:39:38)
at Socket.<anonymous> (/app/node_modules/pg-protocol/dist/index.js:10:42)
at Socket.emit (events.js:315:20)
at addChunk (internal/streams/readable.js:309:12)
at readableAddChunk (internal/streams/readable.js:284:9)
at Socket.Readable.push (internal/streams/readable.js:223:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
my knexfile.js looks like the following.
module.exports = {
development: {
client: 'sqlite3',
connection: {
filename: './data/lessons.db3',
},
useNullAsDefault: true,
pool: {
afterCreate: (conn, done) => {
conn.run('PRAGMA foreign_keys = ON', done);
},
},
},
production: {
client: 'pg',
connection: process.env.DATABASE_URL,
pool: {
min: 2,
max: 10,
},
migrations: {
tablename: 'knex-migrations',
directory: './migrations',
},
},
};
[Node Express Tutorial 18 - Setting up a Postgres database in
Heroku][1]
[1]: https://www.youtube.com/watch?v=OZQWfW3VvhE&list=PLKii3VqdFnoZY6EBxb2K37D0wrEmS-5RD&index=17
I found I could overcome my problem by changing the knexfile.js to something close to what is in the heroku documentation.
module.exports = {
development: {
client: 'sqlite3',
connection: {
filename: './data/lessons.db3',
},
useNullAsDefault: true,
pool: {
afterCreate: (conn, done) => {
conn.run('PRAGMA foreign_keys = ON', done);
},
},
},
production: {
client: 'pg',
connection: {
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false },
},
migrations: {
directory: __dirname + '/migrations',
},
seeds: {
directory: __dirname + '/seeds',
},
},
};
I am follwing this thread on how to get TypeORM and PostgreSQL to work in Heroku.
Using ormconfig.js and module.exports I get this error
MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "cordova", "expo", "mariadb", "mongodb", "mssql", "mysql", "oracle", "postgres", "sqlite", "sqljs", "react-native".
const env = require('dotenv')
env.config()
module.exports = {
name: 'default',
type: process.env.DATABASE_TYPE,
host: process.env.DATABASE_HOST,
port: 5432,
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
synchronize: true,
dropSchema: false,
logging: true,
entities: ['/src/**/*.entity.ts', 'dist/**/*.entity.js'],
extra: {
ssl: true,
},
};
The above link says switch to .ts and use export
so..using ormconfig.ts and export = config I get this error:
2020-03-25T05:07:57.946988+00:00 app[web.1]: export = config
2020-03-25T05:07:57.946988+00:00 app[web.1]: ^^^^^^
2020-03-25T05:07:57.946988+00:00 app[web.1]:
2020-03-25T05:07:57.946989+00:00 app[web.1]: SyntaxError: Unexpected token 'export'
const config = {
name: 'default',
type: process.env.DATABASE_TYPE,
host: process.env.DATABASE_HOST,
port: 5432,
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
synchronize: true,
dropSchema: false,
logging: true,
entities: ['/src/**/*.entity.ts', 'dist/**/*.entity.js'],
extra: {
ssl: true,
},
};
export = config;
I've also tried using export default but had same error as with export...
Not exactly sure where to go from here...anyone had this issues?
does babel need to be included to run my node.js apps in heroku?
Do it this way to get rid of (2020-03-25T05:07:57.946989+00:00 app[web.1]: SyntaxError: Unexpected token 'export') error:
exports.config = {
name: 'default',
type: process.env.DATABASE_TYPE,
host: process.env.DATABASE_HOST,
port: 5432,
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
synchronize: true,
dropSchema: false,
logging: true,
entities: ['/src/**/*.entity.ts', 'dist/**/*.entity.js'],
extra: {
ssl: true,
},
};
ormconfig MUST keep the .js extension.
Also, I am parsing the DATABASE_URL from Heroku and adding all the variables into their appropriate placeholders in ormconfig like so:
const parse = require('pg-connection-string').parse;
const env = require('dotenv')
env.config()
const config = parse(process.env.DATABASE_URL)
const pgConnection = {
type: "postgres",
host: config.host,
port: config.port,
username: config.user,
password: config.password,
database: config.database,
synchronize: true,
dropSchema: false,
logging: true,
entities: ['/src/**/*.entity.ts', 'dist/**/*.entity.js'],
extra: {
ssl: true,
}
}
module.exports = pgConnection;