teslamate-org / teslamate

A self-hosted data logger for your Tesla 🚘
https://docs.teslamate.org
MIT License
6.05k stars 755 forks source link

Add support for socket connection to PostgreSQL #4249

Open onovy opened 1 month ago

onovy commented 1 month ago

Is there an existing issue for this?

What happened?

Currently it's possible to connect to DB only over IP, but PostgreSQL supports Unix-domain socket connection. Can you add support to TeslaMate code + docker and Grafana docker please?

Probably best way to support this is to migrate to connection strings instead of host+port pair in envs.

Thanks.

Expected Behavior

No response

Steps To Reproduce

No response

Relevant log output

not relevant

Screenshots

No response

Additional data

No response

Type of installation

Docker

Version

1.30.1

JakobLichterfeld commented 1 month ago

Thanks for your suggestion.

Do you mean similar to using a reverse Proxy with Unix Domain Sockets with TeslaMate?

Btw. PR always welcome!

brianmay commented 1 month ago

If I am not mistaken I have come across this exact same issue.

He wants Teslamate to connect to postgresql using the socket, in my case that is /run/postgresql/.s.PGSQL.5432.

This results in less overheads (not sure how significant this is), and means we don't need to manage secrets for teslamate (because the database can do authentication based on the Unix user).

Ecto supports connecting via a socket. But unfortunately, AFAIK, ecto does not support connecting to a socket using a URL to the database. Which is unfortunate, it would mean we just need to support a URL and can connect anywhere. Rather the config needs :socket and :socket_dir values.

See https://github.com/elixir-ecto/postgrex/issues/415

Note: need to check this is still correct. It is possible issue was fixed, but but bug report was not updated.

When I setup my new database I ended up using sockets where possible, and TCP using password authentication for the apps where this did not appear to be easy. Currently all my Elixir apps fall into this category.

onovy commented 1 month ago

Do you mean similar to using a reverse Proxy with Unix Domain Sockets with TeslaMate?

nope, that's different think. brianmay explained it exactly :)

My problem is not overhead nor secrets problem. I have PostgreSQL running on bare-metal, outside of docker. And in this setup it's much simpler to "mount" PostgreSQL socket into docker container instead of playing with TCP/IP.

Details:

Thanks.