Can't reach database server (postgres)

Issues information

Your issue

I am trying deploying my first application which is a forked version of umami. After deploying an instance of postgres database and trying to login, I am getting an error in console related to app not being able to connect to the database’s host
PrismaClientInitializationError: Can't reach database server at z7f6c6acf-postgresql-z61b70b01-qovery-cloud:5432``

Dockerfile content (if any)

# Install dependencies only when needed
FROM node:16-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

# Rebuild the source code only when needed
FROM node:16-alpine AS builder
ARG BASE_PATH
ARG DATABASE_TYPE
ENV BASE_PATH=$BASE_PATH
ENV DATABASE_URL "postgresql://umami:umami@db:5432/umami"
ENV DATABASE_TYPE=$DATABASE_TYPE
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
ENV NEXT_TELEMETRY_DISABLED 1

RUN yarn build

# Production image, copy all the files and run next
FROM node:16-alpine AS runner
WORKDIR /app

ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

# You only need to copy next.config.js if you are NOT using the default configuration
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000

CMD ["node", "server.js"]

Hi @waptik ,

I’ve noticed 2 things:

Bad database URL?

Your error message is PrismaClientInitializationError: Can't reach database server at z7f6c6acf-postgresql-z61b70b01-qovery-cloud:5432

But z7f6c6acf-postgresql-z61b70b01-qovery-cloud is not a valid host. How did you get this host?

Dockerfile

I think you need to specify the following in your Dockerfile:

ARG DATABASE_URL
ENV DATABASE_URL $DATABASE_URL

Otherwise, you can’t use the DATABASE_URL environment variable.

I got z7f6c6acf-postgresql-z61b70b01-qovery-cloud from QOVERY_POSTGRESQL_Z7F6C6ACF_HOST environment variable.

The dockerfile already had DATABASE_URL defined at https://github.com/waptik/umami/blob/master/Dockerfile#L15, but I guess I have to replace the one at https://github.com/waptik/umami/blob/master/Dockerfile#L14 with ARG DATABASE_URL.

Your DATABASE_URL has been set manually (cf screenshot), it must be an alias otherwise it will not work correctly across the different environments.

It must be something like this

Please refer to the documentation on how to create aliases for a database.

Thanks so much! That worked.

Now i just have to figure out how to apply the redeploy script in docker because prisma schema is currently not being deployed after the app has been built.