Setting up Postgres with CircleCI for Node

nodejs
postgresql

#1

I’m new to CircleCI and having some trouble with getting Postgres implemented in the config file, albeit I am new to Postgres as well so maybe it’s an error on my part with that tech and not CircleCI.

I input my environment variables via the settings for the repository instead of in the config file. Now the way I have things set to run is that when running yarn test the below piece of code will check what environment Node is operating in and create the appropriate Sequelize database connection:

if (process.env.NODE_ENV === 'test') {
  sequelize = new Sequelize(env.PSQL_URI, { logging: false })
} else if (process.env.NODE_ENV === 'development') {
  sequelize = new Sequelize(env.PSQL_URI)
} else if (process.env.NODE_ENV === 'production') {
  sequelize = new Sequelize(env.PSQL_URI)
}

In my environment.js file, I have separate env.PSQL_URI based on the NODE_ENV

const testConfig = {
  EXPIRE_TIME: '1s',
  JWT_SECRET: 'm3LL0wY3ll0w',
  PSQL_URI: `postgres://${process.env.PSQL_USER}:${process.env
.PSQL_PASSWORD}@localhost:5432/graphql_todo_test`
}

When running locally I have the PSQL_USER set to my local user and the password is set up the same. When I pushed and Circle began the setup I got back:

waiting for server to shut down…LOG: received fast shutdown request
.LOG: aborting any active transactions
LOG: autovacuum launcher shutting down
LOG: shutting down
LOG: database system is shut down
done
server stopped

PostgreSQL init process complete; ready for start up.

LOG: database system was shut down at 2017-07-22 11:26:19 UTC
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
LOG: incomplete startup packet
FATAL: role “rockchalkwushock” does not exist

rockchalkwushock is my local user value that is stored in env.PSQL_USER. I’m guessing that there is either a default value I’m overriding by accident with this or there is a specific user that must be set for this to work.

Any tips or pointers would be greatly appreciated. Liking Circle just lots to learn.


#2

I got it figured out finally. It had to do with how I was providing environment variables in the config.yml and in my code.


#3

You can find the config.yml I am using below, hopefully, this can help others that run into the same problems.

# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
jobs:
  build:
    working_directory: ~/repo
    docker:
      - image: circleci/node:7.10
        environment:
          PGHOST: 127.0.0.1
          PGUSER: rockchalkwushock
          NODE_ENV: test
      - image: circleci/postgres:9.6.2-alpine
        environment:
          POSTGRES_DB: graphql_todo_test
          POSTGRES_PASSWORD: ""
          POSTGRES_USER: rockchalkwushock
    steps:
      - checkout
      # Download and cache dependencies
      - restore_cache:
          keys:
          - repo-yarn-{{ checksum "yarn.lock" }}
          # fallback to using the latest cache if no exact match is found
          - repo-yarn-
      - run: yarn install
      - save_cache:
          paths:
            - ~/.yarn-cache
          key: repo-yarn-{{ checksum "yarn.lock" }}
      # Wait for Postgres connection to open.
      - run: dockerize -wait tcp://localhost:5432 -timeout 1m
      # Run validation checks & generate coverage reports
      - run: yarn start validate.withCoverage
      # Send coverage report to Codecov
      - run: yarn start reportCoverage
      - store_artifacts:
          path: ./coverage/clover.xml
          prefix: tests
      - store_artifacts:
          path: coverage
          prefix: coverage
      - store_test_results:
          path: ./coverage/clover.xml

#4