hasura / graphql-engine

Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
https://hasura.io
Apache License 2.0
31.12k stars 2.77k forks source link

Hasura with postgres connection #9417

Open Vedantjaiswal4352 opened 1 year ago

Vedantjaiswal4352 commented 1 year ago

Hello I was trying to connect Hasura to an already existing database of postgres. I tried all three methods as ENV variables, URL, and connection parameters. note that I have docker of both Hasura and postgres in different Screenshot from 2023-02-06 17-52-53 docker-compose: Following are specifications [ { "definition": "name", "message": "connection to the server at "localhost" (127.0.0.1), port 5432 failed: Connection refused\n\tIs the server running on that host and accepting TCP/IP connections?\nconnection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address\n\tIs the server running on that host and accepting TCP/IP connections?\n", "name": "source name", "reason": "Inconsistent object: connection error", "type": "source" } ]

Both docker containers are running and even with the same parameters I was able to connect pgadmin 4 Also I tried with different posts such as 172.17.0.4 because I was showing this IP too for IPv4 even tried with localhost.enter image description here

I wanted to get connected with Hasura and postgres who both are running as independent containers in docker.

SamirTalwar commented 1 year ago

Sounds like you're running both PostgreSQL and Hasura in different Docker containers, and forwarding the ports.

This means that PostgreSQL will not be available on "localhost". This works for pgAdmin because, I assume, you are running it on the host, not inside a Docker container, and you are forwarding the port to the host.

How you connect these two containers together is dependent on how you started them.

If you are using Docker Compose and the containers are in the same YAML file, you can probably just use the container name as the hostname. For example, if your PostgreSQL container is named "pg", you can use that as the host.

If you are just running containers on the command line, you will need to create a network and add the containers to the network. (Docker Compose does this for you.)

First, create the network:

$ docker network create hasura

Then connect your running PostgreSQL container to the network (assuming the container is named "pg"):

$ docker network connect hasura pg

Finally, start the Hasura GraphQL Engine connected to the same network. If it's already running, do what you did above; if it's not started, you can provide the network name as part of the docker run command:

$ docker run ... --net=hasura ... hasura/graphql-engine ...

You can then use the container name as the hostname, as above.

Vedantjaiswal4352 commented 1 year ago

Actually did exactly the same first started a network and then added both Hasura and postgres to it and then also checked the status and that network but it still gave the same error. Note: I tried with both the standard network and also in swarm mode too. Then I thought it might be because I had installed Hasura and postgres previously so I removed everything about those and started a fresh new container with new version images after setting up everything it was still giving the same error and also I can't add postgres and Hasura in same docker-compose so is there really no other way?

SamirTalwar commented 1 year ago

I'm sorry, I didn't get a notification about your reply.

Can you share the set of commands you're using to run PostgreSQL and Hasura?

rccc commented 1 year ago

@SamirTalwar

Hello,

Can you share a docker image that would let us use a postgres instance installed on host or in another container ?

I am using docker-compose, but then it is not possible to connect to database outside container, cannot use 172.17.0.1, cannot use host.docker.internal... Very frustrating.

For now i am using docker run with env vars.

Regards

SamirTalwar commented 1 year ago

The Docker image should work well, but you will need to configure your networking appropriately. Containers in Docker do not control the network infrastructure, so there's nothing you can do to make an image work differently. You have to configure it.

You can ask Docker (or Docker Compose) to run a container using host networking with --net=host or network_mode: host. Running Hasura with this option will make it share the host networking, which will give it full access to everything running on the host and will bind ports directly.

I don't recommend it, because it makes it very hard to isolate different containers, but in the case where you're running the database directly on the host, it might be the easiest option.