Skip to content

Instantly share code, notes, and snippets.

@keekerdc
Forked from sourdoughdev/config.exs
Created December 6, 2021 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keekerdc/4a2cbdc058f966962732a573c7356e93 to your computer and use it in GitHub Desktop.
Save keekerdc/4a2cbdc058f966962732a573c7356e93 to your computer and use it in GitHub Desktop.
Connecting to PlanetScale DB from Phoenix (elixir)
# config/dev.exs
# Now running migrations will be: PSCALE=1 mix ecto.migrate
db_name = if System.get_env("PSCALE"), do: "db_name", else: "db_name_dev"
db_hostname = if System.get_env("PSCALE"), do: "127.0.0.1", else: "localhost"
db_port = if System.get_env("PSCALE"), do: 3305, else: 3306
# Configure your database
config :db_name, db_name.Repo,
username: "root",
password: "",
database: db_name,
hostname: db_hostname,
port: db_port,
show_sensitive_data_on_connection_error: true,
pool_size: 10
database_name =
System.get_env("DATABASE_NAME") ||
raise """
environment variable DATABASE_NAME is missing.
For example: my_db_name (see planetscale connection guides)
"""
database_hostname =
System.get_env("DATABASE_HOSTNAME") ||
raise """
environment variable DATABASE_HOSTNAME is missing.
For example: random-hostname.region.psdb.cloud (see planetscale connection guides)
"""
database_username =
System.get_env("DATABASE_USERNAME") ||
raise """
environment variable DATABASE_USERNAME is missing.
For example: ie83n0neoerr (see planetscale connection guides)
"""
database_password =
System.get_env("DATABASE_PASSWORD") ||
raise """
environment variable DATABASE_PASSWORD is missing.
For example: pscale_pw_ceT0k5mVVzi4GWPPyOaFORbpf4yGsf (see planetscale connection guides)
"""
config :db_name, AppName.Repo,
username: database_username,
database: database_name,
hostname: database_hostname,
password: database_password,
ssl: true,
ssl_opts: [
verify: :verify_peer,
cacertfile: CAStore.file_path(),
server_name_indication: String.to_charlist(database_hostname),
customize_hostname_check: [
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment