Rake db:migrate fails on pg_dump: not in the path on CircleCI 2.0

ruby
postgresql
2.0

#1

I am not loading a schema because of some restrictions in my gemset. Thus, I need to build my db with a migration.

Here’s the error when circleci runs rake:db:migrate

rake aborted!
failed to execute:
pg_dump -s -x -O -f /home/circleci/classtag-web/db/structure.sql test_app

Please check the output above for any errors and make sure that `pg_dump` is installed in your PATH and has proper permissions.

/home/circleci/classtag-web/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.1/lib/active_record/tasks/postgresql_database_tasks.rb:108:in `run_cmd'
/home/circleci/classtag-web/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.1/lib/active_record/tasks/postgresql_database_tasks.rb:70:in `structure_dump'
/home/circleci/classtag-web/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:217:in `structure_dump'
/home/circleci/classtag-web/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.1/lib/active_record/railties/databases.rake:277:in `block (3 levels) in <top (required)>'
/home/circleci/classtag-web/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.1/lib/active_record/railties/databases.rake:67:in `block (2 levels) in <top (required)>'
/home/circleci/classtag-web/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.1/lib/active_record/railties/databases.rake:59:in `block (2 levels) in <top (required)>'
/home/circleci/classtag-web/vendor/bundle/ruby/2.4.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Tasks: TOP => db:structure:dump
(See full trace by running task with --trace)
Exited with code 1

My config:

version: 2
jobs:
  build:
    working_directory: ~/dir-to-test
    docker:
      - image: circleci/ruby:2.4.1-node
        environment:
          RAILS_ENV: test
      - image: circleci/postgres:9.6-alpine
    steps:
      - checkout

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

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

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

      # Database setup
      - run: bundle exec rake db:create
      - run: bundle exec rake db:migrate

      # Run rspec in parallel
      - type: shell
        command: |
          bundle exec rspec --profile 10 \
                            --format RspecJunitFormatter \
                            --out /tmp/test-results/rspec.xml \
                            --format progress \
                            $(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)

      # Save artifacts
      - type: store_test_results
        path: /tmp/test-results

#2

Hi Jolim,

You can install pg_dump with sudo apt-get install postgresql-client. You can also build this into a docker image to speed up a build, though I suggest optimizing after you get everything working.


#3

Fantastic. I got it to work. It installs postgresql-client 9.4. I’d like to install 9.6 since our production system is 9.6, and if I try using the image circleci/postgres:9.6-alpine, I get a version mismatch error from pg_dump. I’m not sure if postgresql-client 9.6 even is available for ubuntu 14.04, which I what I think we’re using, right? Otherwise, how do I specify it? I’ve tried the normal route (postgresql-client=9.6) but that doesn’t work.


#4

Seconded, can’t install postgresql-client-9.5 or later, for instance. How can we do that ?


#5

I found the solution:
- run:
name: install postgresql-client
command: |
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo sh -c 'echo “deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main” >> /etc/apt/sources.list.d/postgresql.list’
sudo apt-get update; sudo apt-get install postgresql-client-9.5


#6

For 9.6, I did the following:

steps:
  - run: echo 'deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main' | sudo tee /etc/apt/sources.list.d/pgdg.list > /dev/null
  - run: wget --no-check-certificate -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O- | sudo -u root apt-key add -
  - run: sudo apt-get update
  - run: sudo apt-get install postgresql-9.6 postgresql-client-9.6

#7