Unix socket for postgres?

Hello,

I am migrating from MySQL to PostgresQL. My PostgresQL server is setup on my host and vaultwarden in the official docker image. I have setup a volume on /run/postgresql which contain sock file. In my docker compose file I have set up the following value for my DATABASE_URL variable:

postgresql://vaultwarden:a_good_password@unix:/run/postgresql:5432/vaultwarden_db

When I do so, I get the following error message:

[2022-08-29 07:26:09.243][vaultwarden::util][WARN] Can't connect to database, retrying: DieselCon.
[CAUSE] BadConnection(
    "could not translate host name \"unix\" to address: Name or service not known\n",
)

I have found nothing on Internet, any help is welcome. What’s the correct way of connecting to a database via unix socket files?

I don’t think DIesel supports sockets for PostgreSQL.
So i would suggest to use an TCP connection instead.

Came across this today and figured out it is possible to use a Unix-domain socket connection. This is because Diesel’s PGConnection type accepts any vaild libpg connection URI. At the bottom of section 31.1.1.2. Connection URIs you will find:

The host component is interpreted as described for the parameter host. In particular, a Unix-domain socket connection is chosen if the host part is either empty or starts with a slash, otherwise a TCP/IP connection is initiated. Note, however, that the slash is a reserved character in the hierarchical part of the URI. So, to specify a non-standard Unix-domain socket directory, either omit the host specification in the URI and specify the host as a parameter, or percent-encode the path in the host component of the URI:

postgresql:///dbname?host=/var/lib/postgresql
postgresql://%2Fvar%2Flib%2Fpostgresql/dbname

I’m using this to connect vaultwarden to a local postgres instance and it works like a charm. I think this has some benefits over TCP connection because you get around using Postgres user managemant and can instead rely on Linux users, which are a little bit easier to manage. Of course this only applies in a bare metal deployment where you run vaultwarden and postgres as systemd units instead of in docker.