herokucockroachdb

How to connect Heroku Discord Bot to CockroachDB?


I currently have a discord bot built with Nextcord.py with a working database on CockroachDB. I managed to host and deploy the bot on Heroku and commands work, but I can't seem to get the database to work.

The error line in the Heroku log is 2023-06-14T10:29:55.300444+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

When I try the Worker instead of Web, I get this error when I try a command.

2023-06-14T10:46:44.323804+00:00 app[worker.1]: psycopg2.OperationalError: connection to server at "enter server here" (IP), port 32481 failed: Connection timed out
2023-06-14T10:46:44.323805+00:00 app[worker.1]:     Is the server running on that host and accepting TCP/IP connections?
2023-06-14T10:46:44.323806+00:00 app[worker.1]: connection to server at "enter server here" (IP), port 32481 failed: Connection timed out
2023-06-14T10:46:44.323806+00:00 app[worker.1]:     Is the server running on that host and accepting TCP/IP connections?
2023-06-14T10:46:44.323806+00:00 app[worker.1]: connection to server at ""enter server here" (IP), port 32481 failed: Connection timed out
2023-06-14T10:46:44.323807+00:00 app[worker.1]:     Is the server running on that host and accepting TCP/IP connections?

I connect to CockroachDB manually using Psycopg2 using the following code:

conn = psycopg2.connect(
        host = os.getenv('HOST'),
        port = os.getenv('PORT'),
        database = os.getenv('DATABASE'),
        user = os.getenv('USER'),
        password = os.getenv('PASSWORD'),
        sslmode = os.getenv('SSLMODE')
        )

where the the enviorment variables are:

HOST=calmer-herring-5909.7tc.cockroachlabs.cloud
PORT=*port*
DATABASE=*database*
USER=*user*
PASSWORD=*password*
SSLMODE=verify-full

Is this a Heroku related problem? I can't seem to find out what's not working here. Thanks!


Solution

  • I have finally found the solution. I changed the Heroku dyno to Worker instead of Web, and in the connection string, the SSLMODE should be equal to "require", since it is impossible to install the CA certificate that CockroachDB requires make a connection to the database. If you use any other SSLMODE than "required" (either verify-full or disable), it won't work. Hope that helps