Access PostgreSQL 9.6 from Spring Download at CircleCi

I have a Spring boot application that is currently building and running tests in Heroku CI, and I'm trying to get it to work in Circle CI. My configuration file is as follows:

version: 2 jobs: build: docker: - image: circleci/jdk8:0.1.1 - image: postgres:9.6 working_directory: ~/repo environment: # Customize the JVM maximum heap limit JVM_OPTS: -Xmx3200m TERM: dumb steps: - checkout - run: chmod +x gradlew # Download and cache dependencies - restore_cache: keys: - v1-dependencies-{{ checksum "build.gradle" }} # fallback to using the latest cache if no exact match is found - v1-dependencies- - run: ./gradlew dependencies - save_cache: paths: - ~/.m2 key: v1-dependencies-{{ checksum "build.gradle" }} # run tests! - run: ./gradlew test 

I tried various ways to determine DATABASE_URL:

 jobs: build: docker: - image: circleci/jdk8:0.1.1 environment: - DATABASE_URL=postgresql:// dashman_test@localhost :5433/dashman_test - image: postgres:9.6 environment: - POSTGRES_USER=dashman_test - POSTGRES_DB=dashman_test jobs: build: docker: - image: circleci/jdk8:0.1.1 environment: - DATABASE_URL=postgresql:// dashman_test@localhost :5434/dashman_test - image: postgres:9.6 environment: - POSTGRES_USER=dashman_test - POSTGRES_DB=dashman_test jobs: build: docker: - image: circleci/jdk8:0.1.1 environment: DATABASE_URL: postgresql:// dashman_test@localhost :5434/dashman_test - image: postgres:9.6 environment: POSTGRES_USER: dashman_test POSTGRES_DB: dashman_test TEST_DATABASE_URL: postgresql:// ubuntu@localhost /circle_test?sslmode=disable DATABASE_URL: postgresql:// ubuntu@localhost /circle_test?sslmode=disable DATABASE_URL: postgres://ubuntu:@127.0.0.1:5433/circle_test DATABASE_URL: postgres://localhost:5433/dashman_test DATABASE_URL: postgresql:// ubuntu@localhost :5434/circle_test?sslmode=disable DATABASE_URL: postgres://dashman_test: KnDnHtzneyTzps0WuYr35r9@localhost :5433/dashman_test 

Nothing works, I always get this error:

 tech.dashman.dashmanserver.models.AccountTest > create FAILED java.lang.IllegalStateException Caused by: org.springframework.beans.factory.BeanCreationException Caused by: org.springframework.beans.BeanInstantiationException Caused by: org.springframework.beans.factory.BeanCreationException Caused by: org.springframework.beans.BeanInstantiationException Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException tech.dashman.dashmanserver.models.UserTest > create FAILED java.lang.IllegalStateException Caused by: org.springframework.beans.factory.BeanCreationException Caused by: org.springframework.beans.BeanInstantiationException Caused by: org.springframework.beans.factory.BeanCreationException Caused by: org.springframework.beans.BeanInstantiationException Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException tech.dashman.dashmanserver.DashmanserverApplicationTests > contextLoads FAILED java.lang.IllegalStateException Caused by: org.springframework.beans.factory.BeanCreationException Caused by: org.springframework.beans.BeanInstantiationException Caused by: org.springframework.beans.factory.BeanCreationException Caused by: org.springframework.beans.BeanInstantiationException Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException 

What is the correct way to set up a database? I'm a little lost.

+7
java spring spring-boot postgresql circleci
source share
1 answer

Here are some points to help you solve this problem.


(1) The documentation that you mentioned in the comments ( this one ) is either outdated or just misleading. It:

The default user, port, test database for PostgreSQL 9.6 is as follows: Postgres: // ubuntu: @ 127.0.0.1: 5432 / circle_test

... not true .

Actual default values ​​for postgres:9.6 :

  • username: postgres
  • password: <empty>
  • port: 5432
  • database: postgres

You can get a postgres instance from your application at 127.0.0.1 .

You can find more detailed information about the default values here , but there is a catch about setting them (more on this in (3) ).


(2) As far as I know, there is no way to pass usrename \ password to jdbc url for postgres, so you probably have to tell your application not only DATABASE_URL , but also something like DATABASE_USER and DATABASE_PASSWORD .

This part depends on the specifics of your application, but for a typical spring boot application with default database settings, you want to get the following settings:

 spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/postgres spring.datasource.username=postgres spring.datasource.password= 

(3) Alternatively, if your connection settings are hard-coded, you probably want to set up credentials for the postgres instance.

Unfortunately, even when setting POSTGRES_* environment variables when starting the container with docker run works fine, setting them in .circleci/config.yml does not work. There are several open error reports ( 1 , 2 ) describing this or a similar problem, and my money on this is an error.

Fortunately, you can still get around this by installing psql and creating the required user credentials during build (the default credentials still work). Add something like:

  - run: apt-get update -qq && apt-get install -y postgresql - run: command: | psql -h 127.0.0.1 -U postgres -c "CREATE DATABASE databasename;" psql -h 127.0.0.1 -U postgres -c "CREATE USER username WITH PASSWORD 'password'; GRANT ALL PRIVILEGES ON DATABASE databasename TO username;" 

... should do the trick on your steps (see the full example here ).

Using a machine executor to start postgres manually (see the last example on this page ) may also be an option, but I have not tried this myself.


(4) I really tried to set this up for myself, and you can check out the repo with the working version here . Create an example output here .

I used this spring boot example and used it postgres, left only the corresponding tests, added the ci circle and other minor settings. It demonstrates how to configure the application through env. variables and setting up a postgres instance.

The most interesting parts are .circleci/config.yml (where env credentials are defined and the user \ db is created in the postgres instance):

  version: 2
 jobs:
   build:
     docker:
       - image: maven: 3.5.0-jdk-8
         environment:
           DATABASE_URL: jdbc: postgresql: //127.0.0.1: 5432 / databasename
           DATABASE_USER: username
           DATABASE_PASSWORD: password
       - image: postgres: 9.6

     working_directory: ~ / repo

     steps:
       - checkout
       - run: apt-get update -qq && apt-get install -y postgresql
       - run:
           command: |
             psql -h 127.0.0.1 -U postgres -c "CREATE DATABASE databasename;"
             psql -h 127.0.0.1 -U postgres -c "CREATE USER username WITH PASSWORD 'password'; GRANT ALL PRIVILEGES ON DATABASE databasename TO username;"
 - run: mvn test

... and application.properties (where env. variables are used):

  spring.h2.console.enabled = false

 logging.level.org.hibernate.SQL = error

 spring.datasource.url = $ {DATABASE_URL}
 spring.datasource.username = $ {DATABASE_USER}
 spring.datasource.password = $ {DATABASE_PASSWORD}
 spring.jpa.hibernate.ddl-auto = create-drop
 spring.jpa.database = POSTGRESQL
 spring.datasource.platform = postgres
 spring.jpa.show-sql = true
 spring.database.driverClassName = org.postgresql.Driver
+8
source share

All Articles