Using Postgres Docker with Environment Vars

docker
postgresql

#1

We’re migrating from 1.0 to 2.0, and have been porting over our tests, and I am running into an issue. In my yaml file I have this

    docker:
      - image: synqfm/circleci:0.0.1
      - image: postgres:9.6.3
        environment:
          POSTGRES_USER: xxx
          POSTGRES_PASSWORD: yyy
          POSTGRES_DB: xxx_test

However, when I try to run the tests accessing the database, it says it doesn’t exist. (Message : FATAL: role "xxx" does not exist)

Is this expected to work right now? Has anyone successfully used the ENV to do the pre-setup? I could always just run the db commands manually but I thought I could just use some of the built in functionality within the Dockerfile.


#3

Ok, I figured out the problem, its actually a timing issue. When I tried to create the database manually, I started getting a createdb: database creation failed: ERROR: duplicate key value violates unique constraint "pg_database_datname_index" error. So, I had to adjust the “wait” script. Here’s what I used

      - run:
          name: Waiting for Postgres to be ready
          command: |
            for i in `seq 1 10`;
            do
              #nc -z localhost 5432 && echo Success && exit 0
              psql -h localhost -U xxx xxx_test -c "\t" && echo DB Up && exit 0
              echo -n .
              sleep 1
            done
            echo Failed waiting for Postgres && exit 1

#4

I am biased towards this solution;


#5

Thanks, I like that solution, the whole bash script to sleep felt a little wrong to me :slight_smile:


#6

BTW, the “sleep” example actually came from your official documentation here, so it might be nice to show the dockerize model in your docs. One other thing, I believe dockerize only handles raw ports, one thing I wanted our script to do was wait to see the /docker-entrypoint-initdb.d scripts ran properly (db and user created). I didn’t see a way for it to run psql commands or run any checks like that.


#7

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