Cannot connect to databse: Host not found


I’m very new to so please let me know if you need anything from me. I’m trying to set up the database for my app, I did so creating a new postgres instance with flyctl. It worked perfectly and I saved the credentials and the connection string.
I added the connction string as a secret in my app, but now in my app’s log it says that it cannot connect to the database (host not found).
My connection string looks like this:

I have a free plan, so only one org (personal).
Out of curiosity I ran a command from my myapp pod:

root@48edd76c10ee8:/workspace# dig myapp-db.flycast

; <<>> DiG 9.16.1-Ubuntu <<>> myapp-db.flycast
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32772
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0

;edi-db.flycast.                        IN      A

;; Query time: 0 msec
;; SERVER: fdaa::3#53(fdaa::3)
;; WHEN: Tue Jul 18 15:48:56 UTC 2023
;; MSG SIZE  rcvd: 32

I thought everything would go smoothly since I only used the command line tools and didn’t change any configuration…

My app’s fly.toml:

app = "myapp"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

  builder = "heroku/buildpacks:20"

  PORT = "8080"

  auto_rollback = true

  http_checks = []
  internal_port = 8080
  processes = ["app"]
  protocol = "tcp"
  script_checks = []
    hard_limit = 25
    soft_limit = 20
    type = "connections"

    force_https = true
    handlers = ["http"]
    port = 80

    handlers = ["tls", "http"]
    port = 443

    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"

Thank you for any insight

Just spitballing a few ideas since I’ve never actually deployed a Postgres app here, I should probably do that so I have the experience:

  1. Does the database URI need to specify a database? E.g.:
    I’m wondering if maybe URI parsing is getting a junk hostname because it expects a database in the path, and maybe your framework/ORM’s URI parsing isn’t very robust so it errors out in weird ways if it doesn’t get exactly what it expects.
  2. Does the secret seem to be set correctly when connecting via fly console? Likewise via fly ssh console?
  3. Are you able to get a postgres client into the deployed image and connect that way? I’ve occasionally seen database connectivity issues manifest in weird ways, so knowing whether or not a client can actually connect would establish that the issue is likely in the format of the connection string.
1 Like

Thanks for the comment! I dug round your point n°3, and I am indeed able to connect to the database via CLI from the app’s pod! Thanks for pointing in this direction :slight_smile:

I will not write all the investigation process but put it simply it was just a bad copy-paste from my end.

If you use Sequelize, make sure to privilege Option 3 as seen here.. And watch out not to confuse the parameters! The package pg-connection-string makes it a cleaner.

Thank you Nolan!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.