question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: Backend fails to start when using a PostgreSQL connection string

See original GitHub issue

Certain providers inject database connection details into an app as a single connection string environment variable. Backstage configuration supports this via the backend.database.connection.connectionString configuration parameter. When testing this with a PostgreSQL database I however get a startup error.

Expected Behavior

Backend starts.

Current Behavior

On startup, the backend throws the following error and then exits:

Backend failed to start up, Error: The migration directory is corrupt, the following files are missing: 20200511113813_init.js, 20200520140700_location_update_log_table.js, 20200527114117_location_update_log_latest_view.js, 20200702153613_entities.js, 20200721115244_location_update_log_latest_deduplicate.js, 20200805163904_location_update_log_duplication_fix.js, 20200807120600_entitySearch.js, 20200809202832_add_bootstrap_location.js, 20200923104503_case_insensitivity.js, 20201005122705_add_entity_full_name.js, 20201006130744_entity_data_column.js, 20201006203131_entity_remove_redundant_columns.js, 20201007201501_index_entity_search.js, 20201019130742_add_relations_table.js, 20201123205611_relations_table_uniq.js, 20201210185851_fk_index.js

My guess is that the connectionString confuses knex as it tries to run migrations intended for different databases on the same database, causing a conflict/confusion.

Possible Solutions

  • Parse the connectionString and pass different database connections to knex
  • Allow parsing of connection string environment variables in the configuration language
  • Accept workaround of manually editing environment variables (potentially requiring downtime for cloud database maintenance)

Steps to Reproduce

  1. npx @backstage/create-app
  2. Add docker-compose.yml, Dockerfile and .dockerignore (see contents below)
  3. Adjust database config in app-config.yaml to the snippet below.
  4. docker-compose up
  5. Look at console error output from backend
#Dockerfile
FROM node:12-buster

WORKDIR /usr/src/app

COPY package.json yarn.lock lerna.json /usr/src/app/
COPY packages/app/package.json /usr/src/app/packages/app/
COPY packages/backend/package.json /usr/src/app/packages/backend/
RUN yarn install
COPY . /usr/src/app/
RUN yarn tsc
RUN yarn build

CMD ["yarn", "start"]
#docker-compose.yml
version: '3.4'
services:
  backend:
    build: .
    image: backstage
    command: yarn workspace backend start
    ports:
      - '7000:7000'
    links:
      - database
      - lighthouse
    volumes:
      - ./packages/:/usr/src/app/packages
      - ./app-config.yaml:/usr/src/app/app-config.yaml

  app:
    image: backstage
    command: yarn start
    depends_on:
      - backend
    ports:
      - '3000:3000'
    volumes:
      - ./packages/:/usr/src/app/packages
      - ./app-config.yaml:/usr/src/app/app-config.yaml

  lighthouse:
    image: spotify/lighthouse-audit-service:latest
    environment:
      - PGUSER=postgres
      - PGHOST=database
      - PGPASSWORD=secret
      - PGDATABASE=postgres
      - LAS_CORS=true
      - LAS_PORT=3003
    ports:
      - '3003:3003'
    links:
      - database

  database:
    image: postgres
    ports:
      - '5432:5432'
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=secret
#.dockerignore
.git
node_modules
packages/*/node_modules
plugins/*/node_modules
plugins/*/dist
#app-config.yaml snippet
backend:
  database:
    client: pg
    connection:
      connectionString: postgresql://postgres:secret@database:5432/postgres

Context

Cloud database providers often give connection details in a single connectionString environment variable. Furthermore, during maintenance windows they may modify this environment variable automatically to ensure no downtime for the application - if the application uses that specific environment variable. E.g. hardcoding connection details in another format would cause downtime during such a maintenance window.

Your Environment

  • NodeJS Version (v12): v12
  • Operating System and Version (e.g. Ubuntu 14.04): Debian (node:12-buster image)
  • Browser Information: N/A

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
andrewthauercommented, Dec 21, 2020

Oh, I see it right away @erikxiv. It should actually be:

#app-config.yaml snippet
backend:
  database:
    client: pg
    connection: postgresql://postgres:secret@database:5432/postgres

I suspect this isn’t documented, so this ticket can track documentation of the db connection perhaps.

1reaction
frebencommented, Dec 21, 2020

Maybe for starters add examples and clarifying text to the config schema file!

Read more comments on GitHub >

github_iconTop Results From Across the Web

"psql: could not connect to server: Connection refused" Error ...
First, open the PostgreSQL configuration file and make sure that the service is going to listen ... I got the same error when...
Read more >
34.1. Database Connection Control Functions - PostgreSQL
Parses a connection string and returns the resulting options as an array; or returns NULL if there is a problem with the connection...
Read more >
Troubleshoot connections - Azure Database for PostgreSQL
If the application persistently fails to connect to Azure Database for PostgreSQL, it usually indicates an issue with one of the following:.
Read more >
PostgreSQL connection strings - ConnectionStrings.com
Connection strings for PostgreSQL. Connect using Devarts PgSqlConnection, PgOleDb, OleDbConnection, psqlODBC, NpgsqlConnection and ODBC .NET Provider.
Read more >
KB441520:Error occurred when using PostgreSQL database ...
CAUSE: This is caused by a change in a default setting in PostgreSQL starting from version 9.1. standard_conforming_strings ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found