Using psql command in CircleCi 2.0 build



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
  TZ: "/usr/share/zoneinfo/America/Los_Angeles"

    working_directory: ~/circleci-dashboard
      - image: circleci/ruby:2.3-node
          PGUSER: ubuntu
          RAILS_ENV: test
      - image: circleci/postgres:9.6-alpine
          POSTGRES_USER: ubuntu
          POSTGRES_DB: circle_test
      - checkout

      - run:
          name: 'CircleCI dependencies'
          command: bash deploy/

      # 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" }}
          - 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
            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:

Any ideas how can I solve this problem?


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.


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


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:

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.


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`;
              nc -z localhost 5432 && echo Success && exit 0
              echo -n .
              sleep 2
            echo Failed waiting for Postgres && exit 1

I hope it helps!


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
      - image: developmentseed/circleci:node8.4-python2.7
      - image: circleci/postgres:9.6.2-alpine
          POSTGRES_USER: postgres
      - 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


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


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