Phoenix database connection using special characters in the password

Guys, I have a database connection that works that way.
Note: Everything works perfectly

.env

DB_USERNAME="myuser"
DB_PASSWORD="MyPassword@%AAq7*31^^dsaFs"
DB_DATABASE="my_database"
DB_HOSTNAME="127.0.0.1"

prod.exs

#  Configure your database
config :project, Project.RepoTwo,
  username: System.get_env("DB_USERNAME"),
  password: System.get_env("DB_PASSWORD"),
  database: System.get_env("DB_DATABASE"),
  hostname: System.get_env("DB_HOSTNAME"),
  show_sensitive_data_on_connection_error: true,
  pool_size: 3

But I want to change that, I need to leave my prod.exs that way

database_url =
  System.get_env("DATABASE_URL")

config :project, Project.RepoTwo,
  url: database_url,
  show_sensitive_data_on_connection_error: true,
  pool_size: System.get_env("POOL_SIZE") || 10

.env

DATABASE_URL=postgres://myuser:MyPassword@%AAq7*31^^dsaFs@127.0.0.1:5432/my_database

But whenever I make this change it accuses that the password is invalid, I believe it is because of the special characters.

Anyone have any solution for this?

Try to encode password with URI.encode_www_form/1 function

2 Likes

if you notice, it takes the complete url.
postgres://myuser:MyPassword@%AAq7*31^^dsaFs@127.0.0.1:5432/my_database inside .env

Can use
URI.encode_www_form/1
in this complete url?

nope, you have to apply it only on password