How to connect to Postgres via IPv6?
I run Pleroma in FreeBSD jail, connecting to PostgreSQL in another on the same host. The jail has a public IPv6 address and a private IPv4 one. Both IPv6 and IPv4 connectivity to the outside is provided (the latter via NAT).
/etc/resolv.conf is set to a single IPv6 nameserver.
db.saper.info
is defined with an AAAA record in DNS. There is no A entry for it.
Connecting by hand gives the following results:
root@pl:/home/mastodon/live # nc -v -4 db.saper.info 5432
nc: getaddrinfo: hostname nor servname provided, or not known
root@pl:/home/mastodon/live # nc -v -6 db.saper.info 5432
Connection to db.saper.info 5432 port [tcp/postgresql] succeeded!
^C
When I configure Pleroma like this:
# Configure your database
config :pleroma, Pleroma.Repo,
adapter: Ecto.Adapters.Postgres,
username: "pleroma",
password: "******",
database: "pleroma_dev",
hostname: "db.saper.info",
pool_size: 10
It results in:
[info] Starting gopher server on 9999
[error] Process #PID<0.1124.0> raised an exception
** (DBConnection.ConnectionError) connection not available because of disconnection
(db_connection) lib/db_connection.ex:934: DBConnection.checkout/2
(db_connection) lib/db_connection.ex:750: DBConnection.run/3
(db_connection) lib/db_connection.ex:592: DBConnection.prepare_execute/4
(ecto) lib/ecto/adapters/postgres/connection.ex:73: Ecto.Adapters.Postgres.Connection.prepare_execute/5
(ecto) lib/ecto/adapters/sql.ex:256: Ecto.Adapters.SQL.sql_call/6
(ecto) lib/ecto/adapters/sql.ex:426: Ecto.Adapters.SQL.execute_and_cache/7
(ecto) lib/ecto/repo/queryable.ex:133: Ecto.Repo.Queryable.execute/5
(ecto) lib/ecto/repo/queryable.ex:37: Ecto.Repo.Queryable.all/4
(pleroma) lib/pleroma/stats.ex:36: Pleroma.Stats.update_stats/0
[error] Postgrex.Protocol (#PID<0.879.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (db.saper.info:5432): non-existing domain - :nxdomain
It starts working only when I add an IPv4 address for db.saper.info
in /etc/hosts
.
I noticed that gen_tcp has a special option to pick inet6
or inet
and also changes callback module for TCP to inet6_tcp
.
Could this be a reason? How do I tell Postgrex to use inet6 only?
What's interesting, the WebEndpoint binds itself automatically to an IPv6 address and simply works accepting connections from the web frontend server.