使用内存中的PostgreSQL [英] Using in-memory PostgreSQL

查看:91
本文介绍了使用内存中的PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是数据库新手,但我将在Hibernate的应用程序中使用PostgreSQL。这很清楚如何安装Postgres,绑定到休眠并使用它。但我无法弄清楚如何用Maven运行内存中的Postgres数据库进行测试。可能吗?
如果不是 - 任何建议如何在这种情况下测试我的应用程序?



p.s。指向相应教程的链接是最合适的,我将非常感谢他们。
Thanks!

解决方案

(我修改并扩展了这个答案在这篇文章中是一个;我保留在这里,因为这是对这个问题的更直接的回答)。


但我无法弄清楚如何使用maven运行内存中的Postgres数据库进行测试。可能吗?

不,这是不可能的。 PostgreSQL在C中实现并编译为平台代码。你不能将它放在 jar 中,并将它作为一次性内存数据库进行启动。



改为,有一个标准的测试配置和文件。想想:


要运行这些测试,您需要安装PostgreSQL 9.2或更高版本以及在本地主机端口5432上运行的
,名为'myapptest'的用户拥有名为'myapptest'的
a数据库。必须允许'md5'认证
且密码为'abcdefg',或者你可以使用'trust'模式。

要设置这个,你可以使用:

  CREATE USER myapptest PASSWORD'abcdefg'; 
CREATE DATABASE myapptest OWNER myapptest TEMPLATE template0;

并在 pg_hba.conf中添加一个条目例如:

  host myapptest myapptest 127.0.0.1/32 md5 
host myapptest myapptest :: 1/128 md5


使用Maven参数控制是否运行数据库的测试,所以其余的测试仍然可以运行。



你也可以使用Maven params指定一个DB主机名/用户名/ dbname /密码进行测试,渴望。如果你真的热衷于你的测试工具,你可以找到 > initdb postgres 二进制文件,运行 initdb 来创建数据库,修改 pg_hba.conf to trust ,运行 postgres 端口,创建用户,创建数据库并运行测试。我个人认为这是一个应该避免的重大难题;只需要配置一个测试数据库就简单多了。

有些人在PostgreSQL方言模式下使用H2数据库来运行测试。我认为这与使用SQLite进行测试和PostgreSQL进行开发的Rails人差不多。


I'm new to databases, but I'm going to use PostgreSQL in my application with Hibernate. It's rather clear how to install Postgres, bind with hibernate and use it. But I can't figure out how to run in-memory Postgres database with maven for testing. Is it possible? If no - any advises how to test my application in this case?

p.s. links to the corresponding tutorials are the most desirable and I'll highly appreciate them. Thanks!

解决方案

(I've revised and expanded this answer on the post this one is a dup of; I'm retaining this here as it's a more direct answer to this question).

But I can't figure out how to run in-memory Postgres database with maven for testing. Is it possible?

No, it is not possible. PostgreSQL is implemented in C and compiled to platform code. You can't shove it in a jar and fire it up as a throwaway in-memory DB.

Instead, have a standard test config and document it. Think:

To run these tests you'll need PostgreSQL 9.2 or newer installed and running on localhost port 5432, with a user named 'myapptest' who owns a database named 'myapptest'. 'md5' authentication must be permitted with password 'abcdefg', or you can use 'trust' mode.

To set this up you might use:

  CREATE USER myapptest PASSWORD 'abcdefg';
  CREATE DATABASE myapptest OWNER myapptest TEMPLATE template0;

and add an entry in pg_hba.conf like:

  host    myapptest    myapptest    127.0.0.1/32    md5
  host    myapptest    myapptest    ::1/128         md5

Use a Maven parameter to control whether tests that hit the database get run, so that the rest of the tests can still run.

You can probably use Maven params to specify a DB hostname/username/dbname/password for testing too, if you're keen. I wouldn't bother personally.

Alternately, if you're really keen you could have your test harness locate the initdb and postgres binaries, run initdb to create a database, modify pg_hba.conf to trust, run postgres to start it on a random port, create a user, create a DB, and run the tests. Personally I think that's a major pain that should be avoided; it's way easier to just have a test DB configured.

Some people instead use the H2 database in PostgreSQL dialect mode to run tests. I think that's almost as bad as the Rails people using SQLite for testing and PostgreSQL for development.

这篇关于使用内存中的PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆