Home > Mobile >  Python 3.10.6 alembic postgresql password with special character
Python 3.10.6 alembic postgresql password with special character

Time:09-30

migrations/env.py:

f = open("/etc/config.json", "r")
json_config = json.load(f)
config = context.config
section = config.config_ini_section
print(f"password: {json_config['DB_PASSWORD']}, host: {json_config['DB_HOST']}, db: {json_config['DB_DATABASE']}")
config.set_section_option(section, "DB_USERNAME", json_config["DB_USERNAME"])
config.set_section_option(section, "DB_PASSWORD", urllib.parse.quote_plus(json_config["DB_PASSWORD"]))
config.set_section_option(section, "DB_HOST", json_config["DB_HOST"])
config.set_section_option(section, "DB_DATABASE", json_config["DB_DATABASE"])

alembic.ini:

sqlalchemy.url = postgresql://%(DB_USERNAME)s:%(DB_PASSWORD)s@%(DB_HOST)s/%(DB_DATABASE)s

And my password string has @ and $ in it. Running pipenv run alembic revision --autogenerate -m "Initial migration" bumps into the following error:

ValueError: invalid interpolation syntax in 'P@$$w0rd' at position 1

CodePudding user response:

See Alembic.ini passwords also need percent signs doubled where the solution seems to be:

urllib.parse.quote_plus("P@$$w0rd").replace("%", "%%")

In order to escape the encoded %(as %%) so that when Alembic uses configparser to parse the file they are properly handled.

  • Related