Skip to content

Set database on socket string connection #484

Closed

Description

@aurium

node-postgres allows to connect using sockets in a connection string like this:
pg.connect('/some/path', callback)
That is nice, but does not allows to set another database but one with the same user name.

I propose to accept a special protocol named socket to enable the database selection like this:
pg.connect('socket:/some/path?database', callback)
The url.parse('socket:/some/path?database') returns:

{ protocol: 'socket:',
  host: '',
  query: 'database',
  pathname: '/some/path',
  ...}

That data is enough to make the magic, with a small change on function parse at connection-parameters.js. (and also my favorite solution)

That can not change the current behavior for sockets when the first char is "/".


I also try a change on current sockets behavior, splitting the string on spaces. That makes easy to set a database by this way:
pg.connect('/some/path database', callback)

pg/lib/connection-parameters.js
*** 14,20 ****
  var parse = function(str) {
    //unix socket
    if(str.charAt(0) === '/') {
-     return { host: str };
    }
--- 14,21 ----
  var parse = function(str) {
    //unix socket
    if(str.charAt(0) === '/') {
+     var config = str.split(' ');
+     return { host: config[0], database: config[1] };
    }

What you think? node-postgres can use the solution 1, 2, or a mix of both?

I can also code it, if you like.

Activity

  1. brianc commented on Dec 19, 2013

    @brianc
    Owner

    I think that is awesome! 💃 If you code it & it is tested I will merge it! 👍

  2. aurium commented on Dec 19, 2013

    @aurium
    ContributorAuthor

    Ok! Done! :-)
    See pull request #487

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions