Using psql command in CircleCi 2.0 build

docker
circle.yml
2.0

#1

Hello, 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:
          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 5433 -c 'create database survey_builder_test'

It returns the following error:

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

/tmp/.bash_env-59787818c9e77c000144da84-0-build: line 1: ‘/usr/lib/postgresql/9.6/bin/:/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin’: No such file or directory
sudo: psql: command not found
Exited with code 1

I tried solution that are wrote here: https://circleci.com/docs/2.0/postgres-config/.

Any ideas how can I solve this problem?


#2

I added to my script:

- run: sudo apt install postgresql-client

And now 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.


#3

I feel your pain, my friend!
@CircleCI-Employees any solution?


#4

I am facing the same problem but I am still trying to fix it without downgrading to 1.0.

The order the images are called matters and that is the problem we have, you are running psql on the circleci/ruby:2.3-node container, not the circleci/postgres:9.6-alpine.

From here: https://circleci.com/docs/2.0/configuration-reference/#docker--machine-executor

The first image listed in the file defines the primary container image where all steps will run.

entrypoint overrides default entrypoint from Dockerfile.

command will be used as arguments to image entrypoint (if specified in Dockerfile) or as executable (if no entrypoint is provided here or in the Dockerfile).

For primary container (listed first in the list) if no command is specified then command and image entrypoint will be ignored, to avoid errors caused by the entrypoint executable consuming significant resources or exiting prematurely. At this time all steps run in the primary container only.


#5

It is working now =)

The detail is to understand that all commands that we run are executed on the first image and in my case it was a golang image which didn’t have psql installed.

The solution was to install postgresql-client before trying to run psql and another important step is to wait for the database to start. 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

I hope it helps!


#6

Did you come across a solution Mat? I’m having the exact same error and wasn’t able to address with the updates from danielgerep below.

The pertinent part of my config looks like this:

version: 2
jobs:
  build:
    docker:
      - image: developmentseed/circleci:node8.4-python2.7
      - image: circleci/postgres:9.6.2-alpine
        environment:
          POSTGRES_USER: postgres
    steps:
      - checkout

      - run:
          name: Wait for db
          command: dockerize -wait tcp://localhost:5432 -timeout 1m

      - run:
          name: Installing Dependencies
          command: |
            yarn install
            sudo apt-get install postgresql-client

      - run:
          name: Running Test
          command: |
            psql -c "create database test" -U postgres

#7

Updating my psql command to also contain -h localhost -p 5432 resolved this


#8

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.