porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.09k stars 259 forks source link

Unix sockets support on windows #737

Open SvanteRichter opened 8 months ago

SvanteRichter commented 8 months ago

Thanks for the library! Had an issue with what probably is an edge-case, using unix sockets on windows.

UNIX sockets are supported by postgresql on windows (I can start and connect to a database with psql without listenening on any IP), but i cannot connect with postgres.js.

If I try something like

const sql = postgres({
  path: postgresqlServer.socketDir,
  user: 'surface',
  password: 'surface'
 })
 console.log(await sql`SELECT NOW()`)

I get

  Error: connect EPERM C:\Users\surface\Downloads\db\socket
      at PipeConnectWrap.afterConnect [as oncomplete] (node:net:1595:16)
      at cachedError (file:///C:/Users/surface/Downloads/node_modules/postgres/src/query.js:170:23)
      at new Query (file:///C:/Users/surface/Downloads/node_modules/postgres/src/query.js:36:24)
      at sql (file:///C:/Users/surface/Downloads/node_modules/postgres/src/index.js:112:11)
      at TestContext.<anonymous> (file:///C:/Users/surface/Downloads/tests/test-run.test.js:17:23) {
    errno: -4048,
    code: 'EPERM',
    syscall: 'connect',
    address: 'C:\\Users\\surface\\Downloads\\db\\socket'

But if I try with an actual psql.exe it works:

psql.exe -d 'postgres://surface:surface@surface/postgres?host=C:\Users\surface\Downloads\db\socket'

Had a similar issue with node-postgres so opened a issue there too: https://github.com/brianc/node-postgres/issues/3100

porsager commented 8 months ago

Does postgresqlServer.socketDir equal C:\Users\surface\Downloads\db\socket?

If you're using path you need the full url to the actual socket, and not just the dir.

You have two options.

SvanteRichter commented 8 months ago

Thanks for the quick reply!!!

Does postgresqlServer.socketDir equal C:\Users\surface\Downloads\db\socket?

Yes, it does.

Hmm, trying with host:

const sql = postgres({
  host: postgresqlServer.socketDir,
  user: 'surface',
  password: 'surface'
})
  RangeError [ERR_SOCKET_BAD_PORT]: Port should be >= 0 and < 65536. Received type number (NaN).
      at new NodeError (node:internal/errors:406:5)
      at validatePort (node:internal/validators:409:11)
      at lookupAndConnect (node:net:1289:5)
      at Socket.connect (node:net:1246:5)
      at Timeout.connect [as _onTimeout] (file:///C:/Users/surface/Downloads/node_modules/postgres/src/connection.js:345:12) {
    code: 'ERR_SOCKET_BAD_PORT'
  }

Trying with the full path:

const sql = postgres({
  path: postgresqlServer.socketDir + '\\.s.PGSQL.5432',
  user: 'surface',
  password: 'surface'
})
  Error: connect EACCES C:\Users\surface\Downloads\db\socket\.s.PGSQL.5432
      at PipeConnectWrap.afterConnect [as oncomplete] (node:net:1595:16)
      at cachedError (file:///C:/Users/surface/Downloads/node_modules/postgres/src/query.js:170:23)
      at new Query (file:///C:/Users/surface/Downloads/node_modules/postgres/src/query.js:36:24)
      at sql (file:///C:/Users/surface/Downloads/node_modules/postgres/src/index.js:112:11)
      at TestContext.<anonymous> (file:///C:/Users/surface/Downloads/tests/test-run.test.js:22:25) {
    errno: -4092,
    code: 'EACCES',
    syscall: 'connect',
    address: 'C:\\Users\\surface\\Downloads\\db\\socket\\.s.PGSQL.5432'
  }

And if I list the socket to check that it is there it seems like it is:

PS C:\Users\surface\Downloads> ls C:\Users\surface\Downloads\db\socket\.s.PGSQL.5432

    Directory: C:\Users\surface\Downloads\db\socket

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---l        11/23/2023   3:36 AM              0 .s.PGSQL.5432

Both the server and the client are running under the same user, so I don't think it should be a permission issue.