Home > other >  How do I connect Ecto to CockroachDB Serverless?
How do I connect Ecto to CockroachDB Serverless?

Time:10-31

I'd like to use CockroachDB Serverless for my Ecto application. How do I specify the connection string?

I get an error like this when trying to connect.

[error] GenServer #PID<0.295.0> terminating
** (Postgrex.Error) FATAL 08004 (sqlserver_rejected_establishment_of_sqlconnection) codeParamsRoutingFailed: missing cluster name in connection string
    (db_connection 2.4.1) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2

CockroachDB Serverless says to connect by including the cluster name in the connection string, like this:

postgresql://username:<ENTER-PASSWORD>@free-tier.gcp-us-central1.cockroachlabs.cloud:26257/defaultdb?sslmode=verify-full&sslrootcert=$HOME/.postgresql/root.crt&options=--cluster=cluster-name-1234

but I'm not sure how to get Ecto to create this connection string via its configuration.

CodePudding user response:

This configuration allows Ecto to connect to CockroachDB Serverless correctly:

config :myapp, MyApp.repo,
  username: "username",
  password: "xxxx",
  database: "defaultdb",
  hostname: "free-tier.gcp-us-central1.cockroachlabs.cloud",
  port: 26257,
  ssl: true,
  ssl_opts: [
    cert_pem: "foo.pem",
    key_pem:  "bar.pem"
  ],
  show_sensitive_data_on_connection_error: true,
  pool_size: 10,
  parameters: [
    options: "--cluster=cluster-name-1234"
  ]
  • Related