Nodejs Postgress Error no pg_hba.conf entry for host - node.js

I'm creating a new pool object a follows:
import { Pool, PoolConfig } from "pg";
const options = {
host: process.env.DB_HOST,
port: Number.parseInt(process.env.DB_PORT as any),
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
connectionLimit: Number.parseInt(process.env.DB_CONNECTION_LIMIT as any),
ssl: false,
} as PoolConfig;
export const pool = new Pool(options);
But when i try to do pool.query I'm getting the following error:
"There was an error processing your request: no pg_hba.conf entry for host \"172.31.0.1\", user \"admin\", database \"todos\", no encryption"
I've also tried to set the ssl to true but it seems not working.
What maybe posibly the problem here

Related

How to use sequelize to connect to Cloud SQL MySQL Instance? Error: sequelizeconnectionerror: connect ENOENT

I am trying to connect to my database from my Nodejs server like this:
const db = new Sequelize(sql.database, sql.username, sql.password, sql.config);
const connectDB = async () => {
try {
await db.authenticate();
logger.info(`MySQL DB Connected: ${sql.config.host}`)
} catch (error) {
logger.error(`Error: ${error}`);
process.exit(1)
}
}
My configuration looks like:
const sql = {
database: process.env.SQL_DATABASE as string,
username: process.env.SQL_USER as string,
password: process.env.SQL_PASSWORD,
config: {
dialect: "mysql" as Dialect,
host: "/cloudsql/{instance}" as string,
dialectOptions: {
socketPath: "/cloudsql/{instance}",
},
},
};
However I am getting a sequelizeconnectionerror: connect ENOENT
Is there anything I'm missing to connect to my Cloud SQL instance in GCP?
Your sql.config object is missing a "port" key to access the database at. The default port in MySql is 3306.
Depending on your database setup you should also add ssl configuration options.
If you have SSL setup you'd do that by adding a key "ssl" to your dialectOptions with an object containing "key", "ca" and "cert" passing the appropriate certificate to each of them.
Your code would then look like this:
const sql = {
database: process.env.SQL_DATABASE as string,
username: process.env.SQL_USER as string,
password: process.env.SQL_PASSWORD,
config: {
dialect: "mysql" as Dialect,
host: "/cloudsql/{instance}" as string,
port: process.env.SQL_PORT,
dialectOptions: {
socketPath: "/cloudsql/{instance}",
ssl: {
key: process.env.SQL_KEY,
ca: process.env.SQL_CA,
cert: process.env.SQL_CERT
}
},
},
};

Failed to connect to *name_server* - getaddrinfo ENOTFOUND

When connecting to the database with sequelize-typescript, an error occurs
Failed to connect to SERVER\SQL2016:1433 - getaddrinfo ENOTFOUND SERVER\SQL2016
Connection settings
import { Sequelize } from 'sequelize-typescript'
import { environment } from '../config'
import { normalize, join } from 'path'
export default new Sequelize({
database: environment.database.database,
dialect: "mssql",
username: environment.database.username,
// port: environment.database.port,
password: environment.database.password,
host: environment.database.host,
logging: !environment.production ? console.log : false,
models: [normalize(join(__dirname, "..", "models"))],
dialectOptions: {
options: {
enableArithAbort: true,
cryptoCredentialsDetails: {
minVersion: "TLSv1",
},
},
},
})
interface DatabaseConnection {
database: string
username: string
port: number
password: string
host: string
hostAsodu: string
databaseAsodu: string
}
export const environment: Environment = {
port: process.env.PORT ? Number(process.env.PORT) : 3030,
production: process.env.NODE_ENV === "production",
database: {
database: process.env.DB_DATABASE ?? String(),
username: process.env.DB_USERNAME ?? String(),
port: process.env.DB_PORT ? Number(process.env.DB_PORT) : 0,
password: process.env.DB_PASSWORD ?? String(),
host: process.env.DB_HOST ?? String(),
hostAsodu: process.env.DB_HOST_ASODU ?? String(),
databaseAsodu: process.env.DB_DATABASE_ASODU ?? String()
},
}
I tried connectit with and without a port, the error is the same. It just connects to SERVER, but does not want to connect to the named SERVER \ SQL2016. How can I fix this error? Found nothing on the docks
ENOTFOUND is an operating-system-level error from your OS's networking code. It means you asked it to look up a hostname and it came up with nothing. In the lingo, your name "could not be resolved." (www.stackoverflow.com is a hostname, for example. https://www.stackoverflow.com is a URL, which happens to contain a hostname.)
getaddrinfo() is OS method that asks the domain name service (DNS) to look up a hostname.
It looks to me like you tried to look up the hostname SERVER\SQL2016. That's not a hostname. You probably want something like sql2016.example.com instead. Ask the person who operates that SQL Server instance for the correct hosthame.
The SQL Server instance I use has a hostname something like devdatabase.dev.example.com.
Edit The SQL2016 part of your connection string is known as the server instance or DataSource. You need to specify it separately from the hostname. See this. Error connecting to SQL Server database with sequelize You also need to make sure your SQL Server software is configured to allow TCP connections.
Try this as you connect.
export default new Sequelize({
database: environment.database.database,
dialect: "mssql",
username: environment.database.username,
// port: environment.database.port,
password: environment.database.password,
host: environment.database.host, /* should be the hostname without \SQL2016 */
logging: !environment.production ? console.log : false,
models: [normalize(join(__dirname, "..", "models"))],
dialectOptions: {
instanceName: 'SQL2016',
options: {
enableArithAbort: true,
cryptoCredentialsDetails: {
minVersion: "TLSv1",
},
},
},
})

Nodejs connect to Redshift using temporal credentials error

I'd like to connect to Redshift using temporal credentials.
I'd tried connecting with master username and password and it works fine. The problem of the temporal credentials is the username that is following format:
username: 'IAM:awsuser'.
So I think the connection is not understanding correctly the ":". So it always through invalid password. I have try this username and password from the Redshift query-editor and it connects without any problem.
This is the configuration I'm using:
const configRed = {
host: 'redshift-cluster-name.aaaaaaa.eu-west-1.redshift.amazonaws.com',
user: 'IAM:awsuser',
password: data.DbPassword,
database: 'dev',
port: 5439,
idleTimeoutMillis: 0,
max: 10000
};
redshiftPool = new psql.Pool(configRed);
redshiftCon = await redshiftPool.connect();
I have also tried using the username with encodeURIComponent:
user: encodeURIComponent('IAM:awsuser'),
It through next error:
"errorMessage": "password authentication failed for user \"IAM:awsuser\"",
Could be possible to change the connection URL in the PG library, for some custom URL like:
jdbc:redshift:iam://examplecluster.abc123xyz789.us-west-2.redshift.amazonaws.com:5439/dev
Specifying "ssl: true" in the params argument when creating the Pool object indeed works:
const configRed = {
host: 'redshift-cluster-name.aaaaaaa.eu-west-1.redshift.amazonaws.com',
user: 'IAM:awsuser',
password: data.DbPassword,
database: 'dev',
port: 5439,
idleTimeoutMillis: 0,
max: 10000,
ssl: true
};

Login failed using Express and Sequelize with SQL Server

I am trying to use Sequelize (v 5.21.13) to connect to my SQL Server database in my Expressjs app.
dbconfig.js
var dbConfig = {
server: process.env.DB_HOST,
authentication: {
type: 'default',
options: {
userName: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD
}
},
options: {
database: process.env.DB_NAME
}
};
module.exports = dbConfig;
index.js:
const dbConfig = require('./dbConfig');
const Sequelize = require('sequelize');
const connection = new Sequelize(
dbConfig.options.database,
dbConfig.authentication.options.userName,
dbConfig.authentication.options.password,
{
host: dbConfig.server,
dialect: 'mssql',
}
);
connection.sync().then(() => {
console.log('Connected!');
}).catch((e) => {
console.log('Error:\n', e);
});
Now the thing is that each time I run the server, I get this error
AccessDeniedError [SequelizeAccessDeniedError]: Login failed for user 'master'.
I have also tried adding additional properties to the new Sequelize() like the following with no luck.
dialectOptions: {
instanceName: 'instance',
options: {
encrypt: true,
trustServerCertificate: true,
requestTimeout: 30000
}
}
I even tried changing the password to a very simple one with no special characters, connection with Datagrip works after changing but not using Sequelize.
Everything on the dbconfig object is correct so I don't see what the issue might be.
Solved it. I was putting the the db instance id as the database name, I realized that the database name was different. Changed it and I'm now connected through Sequelize.

How to set the Application Name for a Sequelize application

I've got a nodejs application that uses Sequelize as it's ORM. I've successfully got Sequelize connected to the database, but I haven't found anything in the documentation that explains how to set the application name. To clarify I'm looking to set a unique Application Name attribute for my app's connection string. That way when a DBA is looking at traffic they can pick out my application's queries from the rest.
Is this something that Sequelize can even do? Or does this need to be done at the tedious level? Failing that, is there a way in nodejs to specify connection string attributes?
Tedious allows setting the app name with the appName config param. You should be able to set this via the dialectOptions object when creating your Sequelize connection:
var conn = new Sequelize('my_db', 'my_user', 'my_pass', {
host: 'my_server',
dialect: 'mssql',
dialectOptions: {
appName: 'my_app_name'
}
});
For those finding this when they're looking for how to set the name for Postgres, you use application_name in the dialectOptions, eg
{
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
port: process.env.DB_PORT,
host: DB_HOST,
dialect: 'postgresql',
dialectOptions: {
application_name: 'My Node App',
},
},

Resources