Jepzen
Jepzen

Reputation: 3192

Why is my web application using ipv6 to connect to a local postgres

So I been developing a web application which connected to a postgresql server on another machine. Everything works fine.

Then I deployed the web application to the the same machine running the postgresql server and got an error message:

FATAL: 28000: no pg_hba.conf entry for host "fe80::ccee:154f:18f5:418f%11", user "myuser", database "mydb", SSL off 

My pg_hba-conf already has this line:

# IPv6 local connections:
host    all             all             ::1/128                 md5  

Thats supposed to be the loopback address for ipv6 right?

I can fix it by adding this line(pure guessing):

#host   all             all             fe80::/16               md5

I suspect this works by letting everyone connect?

So question is why would it use ipv6 instead of ipv4?

And why does the loopback not work?

Upvotes: 5

Views: 3619

Answers (1)

harmic
harmic

Reputation: 30597

When you connect using a hostname, one of the first things that happens is that libpq (the postgresql client library) will attempt to resolve the hostname. In most cases this will mean looking it up via DNS. Whatever address comes back will then be used for the connection.

I am guessing that in your case the address that the hostname resolves to is an IPv6 address. Your experiments with ping would seem to back up that assumption.

So, despite being on the same machine, it is connecting via the IPv6 address of the host. As far as the server is concerned, it sees the connection coming in from the IPv6 address of the host. This address is not ::1/128 (localhost) so it does not match that entry in pg_hba.conf.

One way to resolve this would be to change the connection string of your appication to localhost (or ::1/128, or even 127.0.0.1). That would cause the loopback entries in the pg_hba.conf to be selected.

If you were using a platform that supports UNIX domain sockets, and as you are connecting to the server on the same host, you would be better off to remove the host parameter from the connection string altogether. In that case libpq would use local UNIX domain sockets to connect to the server, which would be more efficient than connecting locally via an IP address anyway. However since you are using .NET that solution probably does not apply.

Upvotes: 2

Related Questions