Skip to content

Commit

Permalink
fix: handle digital ocean pg connection strings (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForbesLindesay authored May 16, 2020
1 parent f09f53f commit bc72ec2
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/pg/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,17 +596,21 @@ export default function createConnection(
);
}

let requireSslForConnectionString = false;
if (typeof connectionConfig === 'string') {
let url;
try {
url = new URL(connectionConfig);
if (url.searchParams.get('sslmode') === 'require') {
requireSslForConnectionString = true;
}
} catch (ex) {
throw new Error(
'Invalid Postgres connection string, expected a URI: ' +
connectionConfig,
);
}
if (url.protocol !== 'postgres:') {
if (url.protocol !== 'postgres:' && url.protocol !== 'postgresql:') {
throw new Error(
'Invalid Postgres connection string, expected protocol to be "postgres": ' +
connectionConfig,
Expand Down Expand Up @@ -669,7 +673,11 @@ export default function createConnection(
const c =
typeof connectionConfig === 'object'
? {...connectionConfig, ...connectOptions}
: {connectionString: connectionConfig, ...connectOptions};
: {
connectionString: connectionConfig,
...(requireSslForConnectionString ? {ssl: true} : {}),
...connectOptions,
};
const connection = pgp(
c,
noDuplicateDatabaseObjectsWarning ? {v: Math.random()} : undefined,
Expand Down

0 comments on commit bc72ec2

Please sign in to comment.