How to connect to unix socket path in a connection string? #290
-
I'm struggling to get Slonik to work on Google App Engine. On GAE, you connect to the (Cloud SQL) database via a unix socket path. Locally, I can produce such a unix socket that proxies the production database with Google's helper utility
I'm able to connect to this socket with the const client = new pg.Client({
host:"/Users/kelley/cloudsql/my-project-322719:europe-west4:my-project",
user:"postgres",
password:"n4EijI3kEOdkEfFp",
database:"postgres"
}); But, I'm unable to phrase these settings in a single connection string like Slonik requires it. Or maybe, I'm close enough, but at least running into an error I don't understand at all:
Anyone have experience with this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Ooh, found it! :P It took my quite some time, but I found it pretty much right after posting this :P The underlying package that does the connecting string parsing is
Putting this connection string into slonik works :) |
Beta Was this translation helpful? Give feedback.
-
This worked for me ( const username = encodeURIComponent(process.env.DATABASE_USERNAME)
const password = encodeURIComponent(process.env.DATABASE_PASSWORD)
const socketPath = encodeURIComponent(process.env.SOCKET_PATH)
const dbName = encodeURIComponent(process.env.DATABASE_NAME)
const connectionUri = `socket://${username}:${password}@${socketPath}/${dbName}` For example: in Google Cloud, the socket path is If using docker-compose locally, I used custom volumes: version: '3.7'
volumes:
postgresSocketVol:
driver: local
driver_opts:
type: none
o: bind
device: /var/run/postgresql-volume
services:
postgres:
image: postgres:14.5
ports:
- 5400:5432 # Use TCP & port 5400 to connect when using tools like IntelliJ Database Editor (JDBC)
environment:
POSTGRES_DB: db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
volumes:
- postgresSocketVol:/var/run/postgresql And then the socket path would be |
Beta Was this translation helpful? Give feedback.
Ooh, found it! :P It took my quite some time, but I found it pretty much right after posting this :P
The underlying package that does the connecting string parsing is
pg-connection-string
, and those docs specified how I should form it:Putting this connection string into slonik works :)