Setting up Postgres for NodeJS [ Error: "sudo: unknown user: postgres"]

Hi there,

I’m trying to set up postgres for circleci. Currently I need it to run some tests for my seed data, but I can’t get psql work. I attempted to use sudo -u postgres command from the documentation, but it keeps on saying that there is an error. I tried changing the environment user variable, however, that didn’t help.

Any ideas would be greatly appreciated. Here is the current config of my circle.yml:

version: 2
jobs:
build:
docker:
# specify the version you desire here
- image: circleci/node:9.3.0

  # Specify service dependencies here if necessary
  # CircleCI maintains a library of pre-built images
  # documented at https://circleci.com/docs/2.0/circleci-images/
  - image: circleci/postgres:9.6.2-alpine
    environment:
    - POSTGRES_USER: postgres
    - POSTGRES_DB: thumbnail

working_directory: ~/repo

steps:
  - checkout

  # Download and cache dependencies
  - restore_cache:
      keys:
      - v1-dependencies-{{ checksum "package.json" }}
      # fallback to using the latest cache if no exact match is found
      - v1-dependencies-

  - run: npm install

  - save_cache:
      paths:
        - node_modules
      key: v1-dependencies-{{ checksum "package.json" }}

  - run:
    # Add a password to the `ubuntu` user, since Postgres is configured to
    # always ask for a password without sudo, and fails without one.
      command: sudo -u postgres psql -p 5433 -c "create user ubuntu with password 'ubuntu';"
      command: sudo -u postgres psql -p 5433 -c "alter user ubuntu with superuser;"

    # Create a new test database.
      command: sudo -u postgres psql -p 5433 -c "create database test;"

  - run: 
      name: Set Up DB
      command: |
        knex migrate:latest
        knex seed:run
    
  # run tests!
  - run: npm test

Fixed the problem. I was assuming the image wasn’t creating my database, and I wanted to use a script that would have psql create one.

Changed user environment variable:

environment:
- POSTGRES_USER: root // preference change
- POSTGRES_DB: thumbnail

Removed:
- run:
# Add a password to the ubuntu user, since Postgres is configured to
# always ask for a password without sudo, and fails without one.
command: sudo -u postgres psql -p 5433 -c “create user ubuntu with password ‘ubuntu’;”
command: sudo -u postgres psql -p 5433 -c “alter user ubuntu with superuser;”

    # Create a new test database.
      command: sudo -u postgres psql -p 5433 -c "create database test;"

  - run: 
      name: Set Up DB
      command: |
        knex migrate:latest
        knex seed:running_man

Added the knex commands in a script to start the migration and seeding. The sudo wasn’t needed.

Added the process.env in my scripts where they were asking for the user and db to work locally and etc.