Simply replacing % with %%, as previously mentioned, did not work for me because I encountered a password authentication failed error.
To resolve this, I encoded the URL first and then replaced %.
Here’s my final code:
_pg_password = quote_plus(os.getenv("DB_PASSWORD", "default")).replace("%", "%%")