How to properly terminate a dynamic postgrex connection

Hello Elixir friends!

I’m opening a dynamic connection to a database without a Repo using Postgrex.start_link.
After I do my business with it (eg running some queries with Postgrex.query) I’d like to release the connection.
I don’t need to keep state or reopen the connection if it crashes or anything, they’re mostly fire and forget queries.

What would be the best way to do it? Using Process.exit?

I appreciate any help.

1 Like

There is an answer on StackOverflow. We simply do a GenServer.stop(pid).

The Postgrex.start_link/1 documentation states that the DBConnection protocol is used and the db_connection documentation shows us that a GenServer is used. Thus it looks like we can be sure a GenServer is used internally which means using GenServer.stop/1 is safe.