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