Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow all config options when using connection string and object #3327

Open
satire-foxfire opened this issue Oct 9, 2024 · 1 comment

Comments

@satire-foxfire
Copy link

I don't believe there is a way to do this currently, so I believe this would fall more into a feature request than a question, but let me know if there is a way to do this with the current functionality.

Currently you can use both a connection string while still specifying some options like ssl, but other options like user or password don't appear to get merged (they're ignored) unless they're explicitly part of the connection string.

Works:

const connectionString = 'postgres://user:password@hostname:port/database';
const client = new pg.Client({
  connectionString,
  ssl: true,
});

Doesn't work:

const connectionString = 'postgres://hostname:port/database';
const client = new pg.Client({
  connectionString,
  user: 'user',
  password: 'password',
  ssl: true,
});

The alternative I have right now is preparsing the connection string into an instance of URL first, then manually setting url.user = 'user' and url.password = 'password', and then stringifying it before passing it into the client:

const connectionString = 'postgres://hostname:port/database';
const url = new URL(connectionString);

url.user = 'user';
url.password = 'password';

const client = new pg.Client({
  connectionString: String(connectionString),
  ssl: true,
});

It'd be really nice for all the properties to "just work", while keeping the same behavior that anything defined in the connection string directly will take precedence.

@hjr3
Copy link
Contributor

hjr3 commented Nov 16, 2024

We would have to define what happens if you specified the password in the connection string and the object. I imagine that gets complicated.

Two suggestions:

  1. Use query params

If you have postgres://hostname:port/database then you can specify the user and password like this

postgres://@hostname:port/database?user=my_user&password=my_password

For complete safety, this would require us to parse the string into a URL, add the query params and then convert to a string. If you were not concerned with safety then

connectionString + '?user=my_user&password=my_password' is fairly terse.

  1. Parse ahead of time
import { parse } from 'pg-connection-string';

const connectionString = 'postgres://hostname:port/database';
const config = parse(connectionString);
config.user = 'my_user';
config.password = 'my_password';

const client = new pg.Client(config);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants