Rails app can't connect to Postgres server

rails
postgresql

#1

Here’s my .circleci/config.yml. I have a Rails 5.0.3 using webpacker, hence the node/yarn stuff. My issue is that no matter what I try, I get the following error on the rails db:schema:load step. I previously had db:create as a step, but since I set POSTGRES_DB, I was getting an error that the database already exists, so I removed that … which is odd, because presumably it has to be able to connect to determine that the DB exists, however it always fails on db:schema:load. I’ve tried using 127.0.0.1 instead of localhost to no avail. Seems like I’ve been trying different versions of postgres, putting the config in database.yml instead of DATABASE_URL and other attempts at a solution for hours, so it’s time to call on the experts.

PG::ConnectionBad: 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"?
version: 2
jobs:
  build:
    working_directory: ~/myapp
    docker:
      - image: circleci/ruby:2.3-node
        environment:
          DATABASE_URL: "postgres://myapp@localhost/myapp-test"
      - image: circleci/postgres:9.5-alpine
        environment:
          POSTGRES_USER: myapp
          POSTGRES_DB: myapp-test
          POSTGRES_PASSWORD: ""
    steps:
      - checkout

      - type: cache-restore
        name: Restore bundle cache
        key: myapp-bundle-{{ checksum "Gemfile.lock" }}

      - type: cache-restore
        name: Restore yarn cache
        key: myapp-yarn-{{ checksum "yarn.lock" }}

      - run:
          name: Bundle Install
          command: bin/bundle install --path vendor/bundle

      - run:
          name: Yarn Install
          command: yarn install

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

      - type: cache-save
        name: Store yarn cache
        key: myapp-yarn-{{ checksum "yarn.lock" }}
        paths:
          - ~/.yarn-cache

      - run:
          name: Rubocop
          command: bin/rubocop --rails

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

      - run:
          name: Database setup
          command: bin/rails db:schema:load --trace

      - run:
          name: Run tests
          command: bin/rails test

#2

Define the env var PGHOST=127.0.0.1 on container 0.


#3

Thanks, here’s my final, working config.yml. Not sure why this is necessary & isn’t included in the 2.0 docs, but it should be.

version: 2
jobs:
  build:
    working_directory: ~/myapp
    docker:
      - image: circleci/ruby:2.3-node
        environment:
          PGHOST: 127.0.0.1
          PGUSER: myapp-test
          RAILS_ENV: test
      - image: circleci/postgres:9.5-alpine
        environment:
          POSTGRES_USER: myapp
          POSTGRES_DB: myapp-test
          POSTGRES_PASSWORD: ""
    steps:
      - checkout

      - type: cache-restore
        name: Restore bundle cache
        key: myapp-bundle-{{ checksum "Gemfile.lock" }}

      - type: cache-restore
        name: Restore yarn cache
        key: myapp-yarn-{{ checksum "yarn.lock" }}

      - run:
          name: Bundle Install
          command: bin/bundle install --path vendor/bundle

      - run:
          name: Yarn Install
          command: yarn install

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

      - type: cache-save
        name: Store yarn cache
        key: myapp-yarn-{{ checksum "yarn.lock" }}
        paths:
          - ~/.yarn-cache

      - run:
          name: Rubocop
          command: bin/rubocop --rails

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

      - run:
          name: Database setup
          command: bin/rails db:schema:load --trace

      - run:
          name: Run tests
          command: bin/rails test

#4

To be fair, Postgres should better document it.


#5

I’ve never needed this before in the history of my 8 year experience with Rails. DATABASE_URL or database.yml have always sufficed. What’s different here?


#6

Generally you can connect to the socket file, but beyond that, I don’t know specifically what Postgres anticipates. You can either define the env var or define host in your database.yml; as long as host is not null, it will attempt a TCP connection.


#7

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