I am on Heroku and due it’s IP address rotation nature it’s difficult to connect to an external database with IP restriction.
We can use proxy with pg but in sequelize there is no way to add a proxy.
Here is an example code:
const dbConfig = {
host: process.env.DB_HOST || '127.0.0.1',
port: process.env.DB_PORT || 5432
};
const proxyCredentials = {
user: process.env.PROXY_USER,
pass: process.env.PROXY_PASS,
host: process.env.PROXY_HOST,
port: process.env.PROXY_PORT,
};
const establishSocksConnection = async () => {
const connectionInfo = {
proxy: {
ipaddress: proxyCredentials.host,
port: Number(proxyCredentials.port),
type: 5,
authentication: {
username: proxyCredentials.user,
password: proxyCredentials.pass,
},
},
command: 'connect',
destination: dbConfig,
};
const { socket } = await socksPackage.createConnection(connectionInfo);
return socket;
};
const setupDatabase = async () => {
const socketConnection = await establishSocksConnection();
const sequelizeInstance = new SequelizeORM(process.env.DB_URL, {
dialect: 'postgres',
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
},
stream: socketConnection,
},
});
return sequelizeInstance;
};
Sequelize accepts the stream option, but it still uses my local ip when connecting to the DB.
P.s: I am using QuotaGuard Static IP’s on Heroku and trying to connect to the external DB (PostgreSQL on AWS).