I am using the script of the spring.io/spring-roo/#running-from-shell fast guide, a 10 lines example.
The only modification is the jpa setup --provider line, changed to connect PostgreSQL (HIBERNATE --database POSTGRES). All the steps and code are at this roo_hello2pg.md github document.
The application.properties seems
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc\:postgresql\://localhost\:5432/hello2bd
spring.datasource.username=postgres
spring.datasource.password=postgres
What more I need? Some spring.jpa.hibernate lines? The browser generates error "status=500" when use database (insert a value).
As I could see in your gitHub repository, you have configured your connection to the Postgres database correctly.
But did you create the hello2db database and the Timer table in your system?
As the Spring Boot documentation sais, JPA databases will be automatically created only if you use an embedded database (H2, HSQL or Derby)
Check http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-creating-and-dropping-jpa-databases
In your case, to create the database automatically using a Postgres DB, you should include the spring.jpa.hibernate.ddl-auto=create-drop property in the application.properties file.
Hope it helps,
Related
In recent days, I have been exploring the keycloak, Now I want to change the keycloak default keycloak database to PostgreSQL, For that, I have found a document,
In that document, they mention single-line command to change the default keyclaok database to PostgreSQL.
bin/kc.sh start --db postgres --db-url-host localhost --db-username bennison --db-password bennison
But the above command does not work for me. When I run the above command it throws Unknown option: '--db' error.
If you know the solution for the above error, kindly answer.
Is this the only way to change the database from H2 to PostgreSQL or is there any other way?
When I want to use a Mysql database in a Springboot app, I am able to create it on start via a string in properties similar to this:
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/testDb?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
Nevertheless PostgreSQL seems to ignore this:
spring.datasource.jdbc-url=jdbc:postgresql://localhost:5432/testdb?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
Is there a way to create a PostgreSQL db on start of a SpringBoot app before Flyway attempts to initiate tables?
Postgres doesn't support creating a database on demand via the JDBC URL. You can learn about what configuration is possible in the documentation.
I've been looking around Stack Overflow and forums but no luck so far. I have a Spring Boot 2.3.3.RELEASE, JPA/Hibernate stack.
I have a table
CREATE TABLE table_name (...)
I've created the table without quotes and in lower case. It works well in local. In local I am in a Windows env with Amazon Corretto jdk11.0.8_10.
Now I have created an AWS RDS PostreSQL DB instance. It is hosted on a Linux.
When I connect from my Windows to the DB, it works fine. But when I deploy my app on AWS Elastic Beanstalk, when Hibernate tries to query the DB, I have the error
org.postgresql.util.PSQLException: ERROR: relation "table_name" does not exist.
My Entity looks like
#Entity
#Table(name = "table_name")
public class User {
}
The table is in the default public schema.
My application.properties:
spring.datasource.url=jdbc:postgresql://${RDS_HOSTNAME}:${RDS_PORT}/${RDS_DB_NAME}
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.username=${RDS_USERNAME}
spring.datasource.password=${RDS_PASSWORD}
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL10Dialect
spring.jpa.properties.hibernate.globally_quoted_identifiers=false
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.default_schema=public
It doesn't matter if I put the schema or not in the #Table.
The only difference I can spot is the environment. Windows versus Linux. When I launch the app locally and use the AWS RDS DB it works.
For diagnostic problem, you should use plain text at
spring.datasource.url=jdbc:postgresql://${RDS_HOSTNAME}:${RDS_PORT}/${RDS_DB_NAME}
# ...
spring.datasource.username=${RDS_USERNAME}
spring.datasource.password=${RDS_PASSWORD}
What is your version of Spring Boot, PostgreSQL?
This is sample
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres_demo
spring.datasource.username=postgres
spring.datasource.password=123456
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=update
Note: don't use org.hibernate.dialect.PostgreSQL10Dialect , use
spring.jpa.database=org.hibernate.dialect.PostgreSQLDialect
In the end I was using the wrong DB name. I was using postgres as DB name. Instead it was ebdb. I don't understand how I'm allowed to connect via DBeaver or PgAdmin to connect to this postgres or how when running the app locally I was able to connect to this DB name and not when the app was deployed in AWS.
The name is mentionned in the doc: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-rds.html
You can also see the name in the AWS RDS Configuration tab of you DB instance.
Its my first time I am using flyway. My application is spring boot with flyway for db version controlling and postgres as db.
I have write successfully a script V1_1_0__create_schema1.sql for writing schema:
Create schema if not exists schema1;
and second script to create table V1_1_1__create_table.sql for writing table:
create table if not exists schema1.table1( id int NOT NULL, name varchar(255), CONSTRAINT table1_pkey PRIMARY KEY (id) );
Now i want to write sql script for database creation with flyway convention. how can i do it? Thanks for your help!
Is it possible to create db using script in springboot app with flyway
for postgres?
Of course it is :D !
If yes how?
First you will need to add flyway on your project, as a depenency (without a version if you are using spring dependency management or BOM) :
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
Then spring Boot will automatically detect and autowire Flyway with its DataSource and invoke it on startup.
By default flyway looks in src/main/resources/db/migration folder for migration scripts, those script must follow a naming convention as V[version_number]__[Description_of_script].sql, please note that you have 2 underscore, so as example you can have V1__init_db.sql
then you can configure datasource properties in your application.properties ,to activate flyway using :
spring.flyway.enabled=true
spring.flyway.baseline-on-migrate=true
#if you have multiple schemas
spring.flyway.schemas=schema1,schema2
You can have a look at theses resources for more details there is a lot of options to explore there :
database-migrations-with-flyway.
build-a-spring-boot-app-with-flyway-and-postgres.
spring_boot_flyway_database
I'm actually writing a small web application with spring boot and wanted to use a (embedded) H2 database together with Spring Data JPA and Flyway for database migration.
This is my application.properties:
spring.datasource.url=jdbc:h2:~/database;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;
spring.datasource.username=admin
spring.datasource.password=admin
spring.datasource.driver-class-name=org.h2.Driver
In the main() method of my #SpringBootApplication class I do the following:
ResourceBundle applicationProperties = ResourceBundle.getBundle("application");
Flyway flyway = new Flyway();
flyway.setDataSource(applicationProperties.getString("spring.datasource.url"), applicationProperties.getString("spring.datasource.username"), applicationProperties.getString("spring.datasource.password"));
flyway.migrate();
I added a script, which creates a table USER in the database, Flyway says it is correctly migrated, but if I connect to the database, in schema PUBLIC theres only the schema_versions table of Flyway listed.
If I am adding another script, which inserts base data into the USER table, the migration failes, because the table is not present after a restart of my spring boot application.
Can anyone tell me if there is missing in my configuration? Or if there is any wrong assumption in my setup...
I have not enough data about your configuration
Hint:
See migration file must be part of dicrectory /db/migration
Hint
use a pattern like V1.0.1__name.sql 2 under scores
Hint
depending on Flyway version you should start with a sql file version greater than 1.0 example 1.0.1.
Hint per default spring boot jpa drops your database content if you using a in memory database. See http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html section 28.3.3.