pg_net: Can't send HTTP requests to localhost from a trigger function

Bug report

Describe the bug

When trying to send a request from a trigger function, the request doesn’t reach the server. In the _http_response table, the error message is:

Couldn’t connect to server

To Reproduce

Do the following locally:

  1. Create an edge function, and serve it.
  2. Create a trigger function that makes an HTTP request to your edge function:
begin
  perform
    net.http_post(
      url:='url_to_your_localhost_edge_function',
      body:='{}'::jsonb
    ) as request_id;

  return new;
end;
  1. Create a trigger that will execute the function
  2. Trigger the function
  3. You will notice the request did not reach the edge function.

Expected behavior

The request should go through.

Additional context

  • I tried to use both 54321 and 8000 ports
  • When I curl my edge function, it works
  • When I use an external URL instead of localhost, it works

System information

  • OS: macOS

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (2 by maintainers)

Most upvoted comments

This seems related supabase/supabase#9837

Yep, this is unfortunately due to localhost mapping to the Postgres container itself - to reach the Docker host (and all ports exposed on it), either host.docker.internal or 172.17.0.1 have to be used as the target hostname.

@HTMHell could you try using any of the above options to confirm that the http request connects as expected?

Is anyone still seeing this problem in 2024?

image

To be clear, we are directly inputting the host.docker.internal base URL into the supabase dashboard (local instance) right?