denodrivers/postgres

Uncaught (in promise) ConnectionRefused: Connection refused (os error 111)

slim-hmidi opened this issue · 1 comments

I created an app and I upload a tarball inside a platform using google container registry.
Dockerfile:

# Import postgres image
FROM postgres:13.5
RUN apt-get update && apt-get install sudo -y

ADD ./script.sh /docker-entrypoint-initdb.d/script.sh


FROM denoland/deno:1.16.4

# The port that your application listens to.
EXPOSE 5000

# The working directory of the project
WORKDIR /app

# The  username which runs the image
#USER deno

# Cache the dependencies as a layer (the following two steps are re-run only when deps.ts is modified).
# Ideally cache deps.ts will download and compile _all_ external files used in main.ts.
COPY deps.ts .
RUN deno cache --unstable deps.ts

# These steps will be re-run upon each file change in your working directory:
ADD . .

# Compile the main app so that it doesn't need to be compiled each startup/entry.
RUN deno cache --unstable server.ts

# Install denon
RUN deno install -qAf --unstable https://raw.githubusercontent.com/nnmrts/denon/patch-4/denon.ts


CMD ["denon", "start"]

script.sh:

#!/bin/sh
createdb db \
&& psql -U postgres -d db -c "alter user postgres with password 'postgres';";

dbConfig.ts:

import { Database, PostgresConnector } from "https://deno.land/x/denodb@v1.0.40/mod.ts";

const { env } = Deno;

const connector = new PostgresConnector({
  database: "db",
  host:  "127.0.0.1",
  username:  "postgres",
  password:  "postgres",
  port:  5432,
});

const db = new Database({ connector });
export default db;

When the container was executed, I got this issue:

error: Uncaught (in promise) ConnectionRefused: Connection refused (os error 111)
    this.#conn = await Deno.connect(options);
                 ^
    at async Object.connect (deno:ext/net/01_net.js:219:13)
    at async Connection.#createNonTlsConnection (https://deno.land/x/postgres@v0.14.2/connection/connection.ts:223:18)
    at async Connection.#startup (https://deno.land/x/postgres@v0.14.2/connection/connection.ts:280:5)
    at async Connection.startup (https://deno.land/x/postgres@v0.14.2/connection/connection.ts:417:11)
    at async Client.connect (https://deno.land/x/postgres@v0.14.2/client.ts:184:7)
    at async PostgresConnector._makeConnection (https://deno.land/x/denodb@v1.0.40/lib/connectors/postgres-connector.ts:54:5)

How can I fix that?

I'm sure this is a problem with your setup, not with the library itself. Respectfully closing

That said, it's likely your program is running before the database has actually finished initializing. It can take up to a minute, but docker won't wait for the database to finish this process before starting your app. I recommend you read about https://github.com/ufoscout/docker-compose-wait