1

I am using the windows-latest host agent in my azure pipeline to build and package my maven spring boot Java project. As pare of the Maven buil it runs the tests against a postgres db that is contained on the window host agent. However, how can create a database in the postgres server?

I am able to connect to the db by specifing the spring datasource URL, username and password as system properties, that works fine (else an error is shown in the console log that it is unable to connect).

I can use the file schema.sql that contains SQL to perform db initialization that is run by Spring during startup of the app. However, Postgres doesn't have default "creat db if not exists". As such that I tried to use psql to create the db in a script. I found the following command on SO to create the db and tried to run that in a script:

        - script: |
            "C:\Program Files\PostgreSQL\13\bin\psql" -c "CREATE DATABASE default" "user=postgres dbname=postgres password=root"
          displayName: Creating DB

However, it results in the following error in the console log:

ERROR:  syntax error at or near "default"
LINE 1: CREATE DATABASE default
                        ^
##[error]Cmd.exe exited with code '1'.
Finishing: Creating DB

Please some advise on the creation of the db ? And the above error?

The maven task:

        - task: Maven@3
          name: maven_package
          displayName: Maven package
          inputs:
              goals: "package"
              mavenPomFile: "backend/pom.xml"
              options: '--settings backend/.mvn/settings.xml -DmygetUsername=$(mygetUsername) -DmygetPassword=$(mygetPassword) -Dspring.datasource.url=jdbc:postgresql://localhost:5432/default  -Dspring.datasource.username=postgres -Dspring.datasource.password=root'
              mavenOptions: "-Xmx3072m $(MAVEN_OPTS)"
              javaHomeOption: "JDKVersion"
              jdkVersionOption: "1.13"
              mavenAuthenticateFeed: true

1 Answer 1

1

I have reproduced your issue:

enter image description here

The reason for this error is that you cannot create a database named default. default is a reserved word in Postgre. You can click this document for a list of SQL Key Words and whether they are reserved in PostgresSQL.

Sign up to request clarification or add additional context in comments.

1 Comment

Ohhh, thanks, I didn't expect that so I was looking in the wrong direction 🙈. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.