我想创建一个通过JDBC不存在的数据库.与MySQL不同,PostgreSQL不支持create if not exists语法.完成此任务的最佳方法是什么?
应用程序不知道数据库是否存在.它应该检查,如果数据库存在,应该使用它.因此,连接到所需数据库是有意义的,如果由于数据库不存在而导致连接失败,则应创建新数据库(通过连接到默认postgres数据库).我检查了Postgres返回的错误代码,但我找不到任何相同的相关代码.
实现此目的的另一种方法是连接到postgres数据库并检查是否存在所需的数据库并相应地采取措施.第二个是锻炼有点乏味.
有什么方法可以在Postgres中实现这个功能吗?
我正在使用PostgreSQL并且spring-boot-2.0.1希望我的应用程序创建数据库(如果它不存在)。我有以下选项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
Run Code Online (Sandbox Code Playgroud)
上面我收到错误:
Caused by: org.postgresql.util.PSQLException: FATAL: database "mydb" does not exist
Run Code Online (Sandbox Code Playgroud)
知道我错过了什么吗?
我正在开发 spring boot 2 应用程序,并尝试通过配置 hikari 数据源和 spring Jpa 与 postgresql 数据库建立连接。
我在这方面取得了成功,并且我正在使用hibernate.hbm2ddl.autoas update,因此它会在不存在的情况下创建表,但唯一的问题是当架构不存在时它会引发异常
错误
GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement
Caused by: org.postgresql.util.PSQLException: ERROR: schema "test_schema" does not exist
Run Code Online (Sandbox Code Playgroud)
我通过配置类手动配置了一切
配置类
@Bean
@Primary
public HikariDataSource dataSource() {
HikariConfig config = new HikariConfig();
config.setJdbcUrl(databaseUrl);
config.setUsername(username);
config.setPassword(password);
config.setDriverClassName(driverClassName);
config.setConnectionTimeout(connectionTimeout);
config.setIdleTimeout(idleTimeout);
config.setMaximumPoolSize(maxpoolSize);
config.setMaxLifetime(maxLifeTime);
config.setMinimumIdle(minIdleConnections);
//config.setPoolName(poolName);
return new HikariDataSource(config);
}
@Bean
public Properties additionalProps() {
Properties jpaProps = new Properties();
jpaProps.put(hbDialect, "org.hibernate.dialect.PostgreSQLDialect");
jpaProps.put(autoDDL, "update"); …Run Code Online (Sandbox Code Playgroud) 使用H2,
Environment.HBM2DDL_AUTO, "create"
Run Code Online (Sandbox Code Playgroud)
如果数据库尚不存在,则创建数据库.
但是,在Postgres中,不会创建不存在的DB,因此会抛出类似"DB不存在"之类的异常.有没有办法配置Postgres按需创建一个不存在的数据库?
以下配置文件可用于重现该问题:
使用H2工作正常:
package test.postgressql;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.cfg.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@PropertySource("file:C:/springconfig/qpmlib.properties")
@ComponentScan(basePackages = {"test.postgressql"})
@EnableJpaRepositories(basePackages = { "test.postgressql" })
@EnableTransactionManagement
public abstract class H2DBConfig {
@Autowired
org.springframework.core.env.Environment env;
public static final String DB_NAME = getNewDBName();
@Bean
public DataSource dataSource() {
DriverManagerDataSource dmds = new …Run Code Online (Sandbox Code Playgroud) postgresql ×4
database ×2
hibernate ×2
java ×2
spring-boot ×2
ddl ×1
jdbc ×1
orm ×1
spring ×1
spring-data ×1
sql ×1