Mateusz Urbański
Mateusz Urbański

Reputation: 7862

Using psql command in CircleCi 2.0 build

I'm in process of migration from CircleCi 1.0 to CircleCi 2.0. Here is how my circle.yml files look like:

version: 2
environment:
  TZ: "/usr/share/zoneinfo/America/Los_Angeles"

jobs:
  build:
    working_directory: ~/circleci-dashboard
    docker:
      - image: circleci/ruby:2.3-node
        environment:
          PGHOST: 127.0.0.1
          PGUSER: ubuntu
          RAILS_ENV: test
      - image: circleci/postgres:9.6-alpine
        environment:
          POSTGRES_USER: ubuntu
          POSTGRES_DB: circle_test
          POSTGRES_PASSWORD: ""
    steps:
      - checkout

      - run:
          name: 'CircleCI dependencies'
          command: bash deploy/circle-dependencies.sh

      # Restore bundle cache
      - type: cache-restore
        key: dashboard-{{ checksum "Gemfile.lock" }}

      # Bundle install dependencies
      - run: bundle install --path vendor/bundle

      # Store bundle cache
      - type: cache-save
        key: dashboard-{{ checksum "Gemfile.lock" }}
        paths:
          - vendor/bundle

      # Add the Postgres 9.6 binaries to the path.
      - run: echo '/usr/lib/postgresql/9.6/bin/:$PATH' >> $BASH_ENV

      - run: sudo apt install postgresql-client

      - run:
          name: Set up Survey Builder database
          command: sudo psql -p 5433 -c 'create database survey_builder_test'

      - run:
          name: Set up database
          command: |
            bundle exec rake db:create db:schema:load --trace
          environment:
            DATABASE_URL: "postgres://ubuntu@localhost:5432/circle_test"

I have a problem with this command:

  - run:
      name: Set up Survey Builder database
      command: sudo psql -p 5432 -c 'create database survey_builder_test'

and it returns following error:

#!/bin/bash -eo pipefail
sudo psql -p 5432 -c 'create database survey_builder_test'

psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Exited with code 2

Any hints are welcome?

Upvotes: 0

Views: 1070

Answers (1)

user745235
user745235

Reputation:

The command sudo psql -p 5433 -c 'create database survey_builder_test' is being executed on the image circleci/ruby:2.3-node, that's why it was required to install postgresql-client but it is also required to wait for the Postgres service to start and for that, I am using this:

- run:
          name: Waiting for PostgreSQL to start
          command: |
            for i in `seq 1 10`;
            do
              nc -z localhost 5432 && echo Success && exit 0
              echo -n .
              sleep 2
            done
            echo Failed waiting for Postgres && exit 1

Upvotes: 2

Related Questions