将grails连接到postgresql

时间:2016-08-10 11:16:10

标签: postgresql grails intellij-idea

我正在使用带有IntelliJ studio的Grails。如何设置我的application.yml文件以连接到我的postgres版本9.5.3?

1 个答案:

答案 0 :(得分:2)

首先创建数据库,使用与此sql类似的东西:

CREATE USER my_user WITH PASSWORD 'my_secret_password'; 
CREATE DATABASE my_db; 
GRANT ALL ON DATABASE my_db TO my_user;

使用以下命令测试数据库连接:

psql -h localhost -p 5432 -U my_user my_db -W

然后在application.yml文件中使用类似的东西:

environments:
    production:
        dataSource:
            driverClassName: org.postgresql.Driver
            dbCreate: update
            username: my_user
            password: my_secret_password
            dialect: org.hibernate.dialect.PostgreSQLDialect
            url: jdbc:postgresql://db:5432/my_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

假设您的Postgres在localhost和标准端口5432上运行。 由于策略是更新,这将为您创建数据库中的所有表。之后,您可能希望将更新更改为validate,并使用数据库迁移来更改域类。

您可能希望更新用户名,密码和数据库名称

最后在build.gradle文件中,您需要将驱动程序包含为运行时依赖项:

dependencies {
    ...
    runtime 'org.postgresql:postgresql:9.3-1101-jdbc41'
    ...
}