如何在Grails 3.0中配置PostgreSQL?

6

我正在使用IntelliJ IDEA 15.0.2作为IDE。我创建了一个Grails 3.0应用程序,并稍作更改以配置PostgreSQL。

这是我的dataSource:

dataSource:
pooled: true
jmxExport: true
driverClassName: org.postgresql.Driver
username: postgres
password: root

environments:
development:
    dataSource:
        dbCreate: update
        url: jdbc:postgresql://localhost:5432/trace_db
test:
    dataSource:
        dbCreate: update
        url: jdbc:postgresql://localhost:5432/trace_db
production:
    dataSource:
        dbCreate: update
        url: jdbc:postgresql://localhost:5432/trace_db
        properties:
            jmxEnabled: true
            initialSize: 5
            maxActive: 50
            minIdle: 5
            maxIdle: 25
            maxWait: 10000
            maxAge: 600000
            timeBetweenEvictionRunsMillis: 5000
            minEvictableIdleTimeMillis: 60000
            validationQuery: SELECT 1
            validationQueryTimeout: 3
            validationInterval: 15000
            testOnBorrow: true
            testWhileIdle: true
            testOnReturn: false
            jdbcInterceptors: ConnectionState
            defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED

在我的build.gradle中,我添加了runtime "postgresql:postgresql:9.4-1207.jdbc4"

但是当我运行时会出现错误:

ERROR org.apache.tomcat.jdbc.pool.ConnectionPool - Unable to create initial connections of pool.
java.sql.SQLException: org.postgresql.Driver

我错过了什么?

1
你的类路径上是否有PostgreSQL JDBC驱动程序? - Mark Rotteveel
请选择一个答案。 - Pablo Pazos
5个回答

5

数据源

dataSource {
    pooled = true
    jmxExport = true
    driverClassName = "org.postgresql.Driver"
    username = "postgres"
    password = "xxx"

构建配置

dependencies {
        // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g.
        // runtime 'mysql:mysql-connector-java:5.1.29'
        // runtime 'org.postgresql:postgresql:9.3-1101-jdbc41'
        runtime "org.postgresql:postgresql:9.4.1208.jre7"
        test "org.grails:grails-datastore-test-support:1.0.2-grails-2.4"
    }

根据您的数据库号和psql版本更改jre号码。 希望对您有所帮助。干杯!

你救了一个生命!谢谢! - nekiala

1
对于那些寻找Grails 4/5版本的人:
implementation "org.postgresql:postgresql:42.3.2"
dataSource:
pooled: true
jmxExport: true
driverClassName: org.postgresql.Driver
username: postgres
password: ''

最后

url: jdbc:postgresql://localhost:5432/db_name

1

看起来你的配置文件中有一个缺少了制表符

dataSource:
    pooled: true
    jmxExport: true
    driverClassName: org.postgresql.Driver
    username: postgres
    password: root

在数据源后面的所有内容都需要缩进。


0

我不知道是否已经有人回答了,但我发现使用application.yml文件并没有让我访问数据源配置,相反它默认使用了H2驱动程序。

build.gradle文件(仅依赖项块):

compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.grails:grails-core"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails:grails-dependencies"
compile "org.grails:grails-web-boot"
compile "org.grails.plugins:cache"
compile "org.grails.plugins:scaffolding"
compile "org.grails.plugins:hibernate4"
compile "org.hibernate:hibernate-ehcache"

runtime "postgresql:postgresql:9.4.1208-atlassian-hosted"

compile "org.grails.plugins:spring-security-core:3.0.4"

console "org.grails:grails-console"
profile "org.grails.profiles:web:3.1.6"
runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.8.2"

testCompile "org.grails:grails-plugin-testing"
testCompile "org.grails.plugins:geb"
testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"

从application.yml中删除了与数据库相关的所有内容,改为使用application.groovy文件:

dataSource{
    pooled= true
    jmxExport=true
    driverClassName= 'org.postgresql.Driver'
    username= 'xxxx'
    password= 'xxxx'    }

environments{   
    development{
        dataSource{
            dbCreate= 'create-drop'
            url= "jdbc:postgresql://localhost:5432/xxx"
            logSql= true
            hibernate.default_schema= "template_dm" 
        }
    }
    test{
        dataSource{
            dbCreate= 'create-drop'
            url= "jdbc:postgresql://localhost:5432/xxx"
            logSql= true
            hibernate.default_schema= "template_dm" 
        }
    }
    production{
        dataSource{
            dbCreate= 'create-drop'
            url= "jdbc:postgresql://localhost:5432/xxx"
            logSql= true
            hibernate.default_schema= "template_dm" 
        }
    }

}

希望这能帮助所有遇到相同问题的人。
谢谢。

-2

使用grails run-app运行Grails应用程序,而不是通过IntelliJ运行它。我也遇到了这个MySQL问题

我仍然没有弄清楚如何通过IntelliJ正确地工作,但至少在使用Grails控制台时它可以正常工作!

注意:这假定您已经正确设置了JDBC驱动程序。我已经为MySQL正确设置了我的驱动程序,并继续挖掘,认为我可能设置错了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接