Skip to content

Instantly share code, notes, and snippets.

@thomasedgesmith
Last active October 20, 2023 07:55
Show Gist options
  • Save thomasedgesmith/26b1af61e7fa6375623e5bd2c622e43e to your computer and use it in GitHub Desktop.
Save thomasedgesmith/26b1af61e7fa6375623e5bd2c622e43e to your computer and use it in GitHub Desktop.
Connecting to PlanetScale DB from Phoenix (elixir)
hostname = "random-host-name.region.psdb.cloud"
{:ok, pid} = MyXQL.start_link(username: "random-username",
database: "db_name",
hostname: hostname,
password: "************",
ssl: true,
ssl_opts: [
verify: :verify_peer,
cacertfile: CAStore.file_path(),
server_name_indication: String.to_charlist(hostname),
customize_hostname_check: [
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
]
]
)
# I've opted to use port `3305`, since normally it'd just be `3306` locally.
pscale connect db_name --port=3305
PSCALE=1 mix ecto.migrate
# creating a new branch
pscale branch switch branch_name --database db_name --create
# 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