mirror of
https://github.com/sqlchat/sqlchat.git
synced 2025-09-25 09:03:43 +08:00
fix: cannot connect non SSL PostgreSQL (#109)
This commit is contained in:
@ -23,14 +23,30 @@ const newPostgresClient = async (connection: Connection) => {
|
||||
key: connection.ssl?.key,
|
||||
};
|
||||
} else {
|
||||
// rejectUnauthorized=false to infer sslmode=prefer since hosted PG venders have SSL enabled.
|
||||
clientConfig.ssl = {
|
||||
rejectUnauthorized: false,
|
||||
};
|
||||
}
|
||||
|
||||
let client = new Client(clientConfig);
|
||||
await client.connect();
|
||||
|
||||
if (connection.ssl) {
|
||||
await client.connect();
|
||||
} else {
|
||||
try {
|
||||
await client.connect();
|
||||
} catch (error) {
|
||||
// Because node-postgres didn't implement `sslmode: preferred`. So first try to connect via SSL, otherwise connect via non-SSL.
|
||||
// Connecting postgres via non-ssl requires `clientConfig.ssl` is undefined. ref: https://github.com/sqlchat/sqlchat/issues/108
|
||||
if (error instanceof Error && error.message.includes("The server does not support SSL connections")) {
|
||||
clientConfig.ssl = undefined;
|
||||
client = new Client(clientConfig);
|
||||
await client.connect();
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
return client;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user