pyinfra-dev / pyinfra

pyinfra turns Python code into shell commands and runs them on your servers. Execute ad-hoc commands and write declarative operations. Target SSH servers, local machine and Docker containers. Fast and scales from one server to thousands.
https://pyinfra.com
MIT License
3.92k stars 382 forks source link

Add psql_database argument postgres operations/facts #1215

Closed hamishfagg closed 1 week ago

hamishfagg commented 2 months ago

This PR adds the ability to specify which database psql should connect to for postgres/postgresql operaitons and facts.

I can into an issue where I wanted to create a postgres role my_role with CREATEDB, and then connect as that new user to create a database that they owned. Because postgres defaults to connecting to the db with the same name as the role, I would get an error like: FATAL: database "my_role" does not exist.

This method is necessary when using managed postgres services from cloud providers that don't provide superuser access, meaning you can't change the owner of a database.

hamishfagg commented 4 days ago

Hey no problem, thanks for getting to it!