question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Querystring in connection string

See original GitHub issue

The problem

Postgres supports a bunch of stuff in connection strings: https://www.postgresql.org/docs/9.5/static/libpq-connect.html#LIBPQ-CONNSTRING

However, ConnectionParameters, pg-connection-string and pg.Pool’s config seem to only be aware about certain limited subset (like ssl, fallback_app_name and others). Doesn’t seem practical to validate those in node, since server will already reject invalid config (I think).

Usecase

Instead of running a bunch of set X to Y after connecting to postgres on every client, I’d like to specify some session settings on connection string. In particular, I’d like to reproduce the following psql invocation from the node:

psql postgresql://?options=-c%20timezone%3DEurope/Moscow

Solution

Probably parse the whole query string and pass everything we found in it to the server. Not sure how connection flow works, and how to use those parsed params.

Looks like there are a bunch of places need to be change in order to achieve this. Not sure if these are all of them:

  • pg-connection-string’s parse function;
  • pg.Client;
  • pg.ConnectionParameters;
  • pg.Pool’s constructor
  • anything else?

I would be happy to PR this, but I’m not sure where to start. Any tips?

As a sidenote, would be neat to support this stuff: psql options="-c\ timezone=Europe/Moscow".

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
brianccommented, Jul 26, 2016

Sure…so how connection works is

pg authenticates with the database

after authentication pg sends a bunch of settings to establish the session and configure the application name and other stuff…

It does so by first getting the bundle of settings to send: https://github.com/brianc/node-postgres/blob/master/lib/client.js#L212 And then sending these settings via the connection as key-value pairs https://github.com/brianc/node-postgres/blob/master/lib/client.js#L65 https://github.com/brianc/node-postgres/blob/master/lib/connection.js#L143

So modifying the client.getStartupConf() to be smarter is a good place to smart - and then you gotta walk back the various ways configs can be passed to the client/pool and make sure they all accept more parameters…once the client is aware of the object containing the parameters to send to the backend, actually sending them is pretty straight forward.


As for the native connection we take the config object and turn it into a “keyword/value connection string” here:

https://github.com/brianc/node-postgres/blob/master/lib/connection-parameters.js#L74

That string is then passed unmodified to libpq where its converted into properties & sent to the postgres backend by libpq. The reason for doing a dns lookup in connection-parameters is because libpq is mostly non blocking but AFAIK it still does a blocking dns lookup if you connect with a hostname, which can cause big problems in node if you block the event loop in C, so we use node to lookup some DNS info before calling libpq to prevent this.

1reaction
elmigrantocommented, Jul 26, 2016

I’d like to look into this, PR for pg-connection-string seems pretty straight-forward, but I am not sure what stuff to change inside pg. The other problem is that parsing of query string is something psql (or libpq?) does on its own, and I don’t know how those parameters make its way to Postgres server.

I’ll try to dig into this on my own some time, but would be nice if you (or someone else) could maybe explain connection flow here, if that’s not too much hassle.

whitelist

That’s the word I was looking for! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Use Query Strings In ASP.NET - C# Corner
A query string is a collection of characters input to a computer or web browser. A Query String is helpful when we want...
Read more >
What is Query String - Net-Informations.Com
Query String is a group of keywords that send request to the web server. These requests are used to pass information (parameters) from...
Read more >
Using connection string keywords - SQL Server Native Client
Some SQL Server Native Client APIs use connection strings to specify connection attributes. Connection strings are keyword/value pairs.
Read more >
SQL Server entry using URL querystring - Stack Overflow
To get a variable form URL use. Request.QueryString["variable"];. So you can use SqlDataSource to establish SQL Connection or SqlConnection.
Read more >
Query string - Wikipedia
A query string is a part of a uniform resource locator (URL) that assigns values to specified parameters. A query string commonly includes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found