Can't connect to local postgres database (using Postgres.app for MacOS)
The tutorial for "new database" works fine: https://www.prisma.io/docs/get-started/01-setting-up-prisma-new-database-a002/
But the tutorial for "existing database" https://www.prisma.io/docs/get-started/01-setting-up-prisma-existing-database-a003/ gives connection error.
docker-compose.yml:
version: '3'
services:
prisma:
image: prismagraphql/prisma:1.16
restart: always
ports:
- "4466:4466"
environment:
PRISMA_CONFIG: |
port: 4466
databases:
default:
connector: postgres
host: localhost
port: 5432
database: cars
user: john
schema: public
migrations: false
ssl: false
(also tried password: and password: '')
docker-compose up -d
docker logs my_container gives:
WARNING: ConnectException occurred while connecting to localhost:5432
java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.postgresql.core.PGStream.<init>(PGStream.java:69)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:156)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
at org.postgresql.Driver.makeConnection(Driver.java:452)
at org.postgresql.Driver.connect(Driver.java:254)
at slick.jdbc.DriverDataSource.getConnection(DriverDataSource.scala:101)
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:341)
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:193)
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:430)
at com.zaxxer.hikari.pool.HikariPool.access$500(HikariPool.java:64)
at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:570)
at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:563)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Sep 18, 2018 4:26:41 PM org.postgresql.Driver connect
SEVERE: Connection error:
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:245)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
at org.postgresql.Driver.makeConnection(Driver.java:452)
at org.postgresql.Driver.connect(Driver.java:254)
at slick.jdbc.DriverDataSource.getConnection(DriverDataSource.scala:101)
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:341)
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:193)
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:430)
at com.zaxxer.hikari.pool.HikariPool.access$500(HikariPool.java:64)
at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:570)
at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:563)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.postgresql.core.PGStream.<init>(PGStream.java:69)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:156)
Connecting with psql from terminal works fine:
psql -h localhost -p 5432 -U john -d cars
It's a long shot, but any tips in trouble shooting at least?
"localhost" as host won't work when connecting to localhost from docker. It's different based on os, but I've seen this work: "host.docker.internal". If that doesn't work, google "connect to local postgres from docker".
Also, is docker set to receive connections from other than localhost? listen_addresses = '*'
thanks @jamesmbowler , host.docker.internal worked like magic!
One suggestion could be to add the comment about host.docker.internal on the "existing database" tutorial https://www.prisma.io/docs/get-started/01-setting-up-prisma-existing-database-a003/ . This since probably more people that are unfamiliar with docker like me and wants to test prisma with external database almost for certain will run into the same problem here, no?
You're welcome! I don't work here, but sure :)
Thanks for the answer @jamesmbowler! I agree with your suggestion @Cottin, this should be added to the documentation.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Hey, so host.docker.internal apparently only works on Windows and Mac, see here: https://github.com/docker/for-linux/issues/264
I spent a long time trying to figure this out, and it would be great to put that in the documentation somehow. My workaround, and I'm not sure how generally applicable this is, is to set the host to 172.17.0.1.
Right, I'm on Linux, too. You mean 172.17.0.1, right?
I tried 172.17.0.1 but prisma still cannot connect to local postgres. prisma log says "connect timed out"
Most helpful comment
"localhost" as host won't work when connecting to localhost from docker. It's different based on os, but I've seen this work: "host.docker.internal". If that doesn't work, google "connect to local postgres from docker".
Also, is docker set to receive connections from other than localhost? listen_addresses = '*'