Cannot connect Node.js/Knex app to Postgres db

So this is my first time deploying (outside of Heroku) and haven’t had any luck. I was able to deploy my project through the fly launch, deploy, and added my secrets to the fly.toml file under:

[env] 
PORT = "3000"
NODE_ENV = "production"

My knex File looks like this:

if (process.env.DATABASE_URL) {
  pg.defaults.ssl = { rejectUnauthorized: false };
}

production: {
    client: "pg",
    migrations: { directory: "./database/migrations" },
    seeds: { directory: "./database/seeds" },
    connection: process.env.DATABASE_URL,
    pool: { min: 2, max: 10 },
  }

My dbConfig file includes

 const: 
environment = process.env.NODE_ENV || "development";

After deploying the app I received this:

*Postgres cluster nodeexpress-graphql-db created*
*  Username:    postgres*
*  Password:      <PASSWORD>*
*  Hostname:    nodeexpress-graphql-db.internal*
*  Flycast:    <HIDDEN>*
*  Proxy port:  5432*
*  Postgres port:  5433*
*  Connection string: postgres://postgres:<PASSWORD>@nodeexpress-graphql-db.flycast:5432*


*Connect to postgres*
*Any app within the <MY> organization can connect to this Postgres using the above connection string*

*Postgres cluster nodeexpress-graphql-db is now attached to nodeexpress-graphql*
*The following secret was added to nodeexpress-graphql:*
*  DATABASE_URL=postgres://nodeexpress_graphql:<PASSWORD (difference from db above)>@nodeexpress-graphql-db.flycast:5432/nodeexpress_graphql?sslmode=disable*
*Postgres cluster nodeexpress-graphql-db is now attached to nodeexpress-graphql*

On my .ENV file I have PORT=3000, i added the DATABASE_URL above, but when I try to enter the credentials on the PG app, for the name, I’m confused, is the name nodeexpress-graphql-db? Or just nodeexpress_graphql? Notice aboe, the DATABASE_URL has nodeexpress_graphql with an underscore so there’s two similar ones. I tried entering the credentials above and when i hit save on the pg admin app, i get this: “unable to connect to server: could not translate host name “nodeexpress-graphql-db.internal” to address: Unkown host”

Isn’t the ‘internal’’ name the host name? I’m not sure what I’m doing wrong, again, any help is greatly appreciated.

Hi… Is nodeexpress-graphql giving connection errors when it runs? (If so, please do post the logs.)

If the aim instead was to connect a local GUI application for a bit of initial tweaking, then the following usually provides the path of least resistance:

https://fly.io/docs/postgres/connecting/connecting-with-flyctl/

The .internal and .flycast addresses are not accessible by default from your local machine.


Aside: Be wary of user names vs. host names in postgres:// URLs, :dragon:

1 Like
╰─± /root/.fly/bin/flyctl logs -a nodeexpress-graphql           
2023-12-05T21:09:15Z proxy[080e710a616408] ewr [info]Starting machine
2023-12-05T21:09:15Z app[080e710a616408] ewr [info][    0.051898] PCI: Fatal: No config space access function found
2023-12-05T21:09:15Z app[080e710a616408] ewr [info] INFO Starting init (commit: 15238e9)...
2023-12-05T21:09:15Z app[080e710a616408] ewr [info] INFO Preparing to run: `docker-entrypoint.sh npm run start` as root
2023-12-05T21:09:15Z app[080e710a616408] ewr [info] INFO [fly api proxy] listening at /.fly/api
2023-12-05T21:09:15Z app[080e710a616408] ewr [info]2023/12/05 21:09:15 listening on [fdaa:3:bb7d:a7b:15e:428b:c692:2]:22 (DNS: [fdaa::3]:53)
2023-12-05T21:09:15Z proxy[080e710a616408] ewr [info]machine started in 511.803735ms
2023-12-05T21:09:16Z app[080e710a616408] ewr [info]> nodeExpress-graphql@1.0.0 start
2023-12-05T21:09:16Z app[080e710a616408] ewr [info]> node index.js
2023-12-05T21:09:17Z app[080e710a616408] ewr [info]Server is running in port...3000
2023-12-05T21:09:18Z proxy[080e710a616408] ewr [info]machine became reachable in 2.414243148s
2023-12-05T21:14:21Z proxy[080e710a616408] ewr [info]Downscaling app nodeexpress-graphql from 1 machines to 0 machines, stopping machine 080e710a616408 (region=ewr, process group=app)
2023-12-05T21:14:21Z app[080e710a616408] ewr [info] INFO Sending signal SIGINT to main child process w/ PID 306
2023-12-05T21:14:26Z app[080e710a616408] ewr [info] INFO Sending signal SIGTERM to main child process w/ PID 306
2023-12-05T21:14:31Z app[080e710a616408] ewr [warn]Virtual machine exited abruptly
2023-12-06T05:41:20Z proxy[080e710a616408] ewr [info]Starting machine
2023-12-06T05:41:20Z app[080e710a616408] ewr [info][    0.046778] PCI: Fatal: No config space access function found
2023-12-06T05:41:21Z app[080e710a616408] ewr [info] INFO Starting init (commit: 15238e9)...
2023-12-06T05:41:21Z app[080e710a616408] ewr [info] INFO Preparing to run: `docker-entrypoint.sh npm run start` as root
2023-12-06T05:41:21Z app[080e710a616408] ewr [info] INFO [fly api proxy] listening at /.fly/api
2023-12-06T05:41:21Z app[080e710a616408] ewr [info]2023/12/06 05:41:21 listening on [fdaa:3:bb7d:a7b:15e:428b:c692:2]:22 (DNS: [fdaa::3]:53)
2023-12-06T05:41:21Z proxy[080e710a616408] ewr [info]machine started in 515.304325ms
2023-12-06T05:41:22Z app[080e710a616408] ewr [info]> nodeExpress-graphql@1.0.0 start
2023-12-06T05:41:22Z app[080e710a616408] ewr [info]> node index.js
2023-12-06T05:41:22Z app[080e710a616408] ewr [info]Server is running in port...3000
2023-12-06T05:47:30Z proxy[080e710a616408] ewr [info]Downscaling app nodeexpress-graphql from 1 machines to 0 machines, stopping machine 080e710a616408 (region=ewr, process group=app)
2023-12-06T05:47:30Z app[080e710a616408] ewr [info] INFO Sending signal SIGINT to main child process w/ PID 306
2023-12-06T05:47:35Z app[080e710a616408] ewr [info] INFO Sending signal SIGTERM to main child process w/ PID 306
2023-12-06T05:47:40Z app[080e710a616408] ewr [warn]Virtual machine exited abruptly

My Dockerfile has EXPOSE 3000, but my fly.toml file has port 8080, i might be a little confused here, this is what I have on the fly.toml file:

# fly.toml app configuration file generated for nodeexpress-graphql on 2023-12-04T14:37:11-06:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "nodeexpress-graphql"
primary_region = "ewr"

[build]

[http_service]
  internal_port = 3000
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ["app"]

[[vm]]
  cpu_kind = "shared"
  cpus = 1
  memory_mb = 1024

[env]
  PORT = "8080"
  NODE_ENV = "production"

[deploy]
  release_command = "knex migrate:latest"

I did add a deploy option on the file I wanted to run the migrations and seeds. I’m able to go into my nodeexpress-graphql-db when I connect to it but none of the migrations worked so i see an empty database where i can add the tables and such, but i thought knex was going to automatically add the migrations for me, this is where I’m currently stuck I guess.

Thanks for the additional details… I’ve added the nodejs tag to this thread, since it might turn out to primarily revolve around the conventions of Knex (which I don’t have experience with myself).

A couple generic standbys you could try out, though:

$ fly pg connect -a nodeexpress-graphql-db --database nodeexpress_graphql
# -- list databases (there should be multiple)...
# \l
# -- report *current* database (should be `nodeexpress_graphql` and not `postgres`)...
# \conninfo
# -- list tables in this database...
# \dt

And:

$ fly ssh console -a nodeexpress-graphql
# find / -name knex
# knex migrate:latest  # error messages (or indeed any other output)

I assume you are running your pg admin app on your laptop/desktop? Fly postgres is running on a private network. If you want to connect to it from your laptop, you will need to use a VPN: Private Networking · Fly Docs

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