Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Postgres database if it doesn't exist when running spring boot application

I'm using PostgreSQL and spring-boot-2.0.1 and want my app to create the database if it doesn't exist. I have the below options in my application.properties

spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL94Dialect

spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=postgres
spring.datasource.password=123

with the above i'm getting error:

Caused by: org.postgresql.util.PSQLException: FATAL: database "mydb" does not exist

any idea what i'm missing?

like image 988
Ahmed Lotfy Avatar asked Jul 04 '18 12:07

Ahmed Lotfy


People also ask

Can spring boot create database if not exists?

Can spring boot create database? 86.3 Initialize a Database Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts). It loads SQL from the standard root classpath locations: schema. sql and data.

Does postgres database always exist?

The postgres database is also created when a database cluster is initialized. This database is meant as a default database for users and applications to connect to. It is simply a copy of template1 and can be dropped and recreated if necessary. So it's a reasonable assumption, but not a bullet-proof assumption.

How do I connect postgres to spring boot?

Using PostgreSQL in Spring Boot Also, we can use JPA of spring data to connect the database of PostgreSQL. Also, we need to add the JDBC driver dependency of the PostgreSQL database to allow the spring boot application to connect or talk with the PostgreSQL database server.


2 Answers

Since all Postgres installations come with a default database, technically it should be possible to connect to it at the very beginning (when the application starts) and then to call

CREATE DATABASE

This is something you could do programmatically before spring initializes, but after the properties are read, for example in Environment Post Processor

The so-early invocation is required because you probably want to take advantage of the properties read by spring boot on one hand, but by the time the spring boot beans will start to get the newly created database in the "operational" state.

So, technically its possible. However, I can't see a real use-case for this scenario. If it's for tests, then you can perfectly use the default Postgres database, that was mentioned in the very beginning of the question.

If it's for production environment - usually DBAs won't allow that, because in order to be able to run that CREATE DATABASE statement, the user that connects to Postgres (spring boot application in this case) should have a very "strong" createdb priviledge, something that DBA won't really want to share.

like image 104
Mark Bramnik Avatar answered Nov 03 '22 18:11

Mark Bramnik


You can create a database if it doesn't exist with spring boot. At least with MySQL, we do:

spring.datasource.url=jdbc:mysql://localhost:3306/mydb?createDatabaseIfNotExist=true

So it should be the same with PostgreSQL:

spring.datasource.url= jdbc:postgresql://localhost:5432/mydb?createDatabaseIfNotExist=true

This doesn't work with PostgreSQL.

like image 4
ISlimani Avatar answered Nov 03 '22 18:11

ISlimani