仅在内存中运行PostgreSQL [英] Running PostgreSQL in memory only

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

问题描述

对于我编写的每个单元测试,我都希望运行一个仅在内存中运行的小型PostgreSQL数据库.例如:

I want to run a small PostgreSQL database which runs in memory only, for each unit test I write. For instance:

@Before
void setUp() {
    String port = runPostgresOnRandomPort();
    connectTo("postgres://localhost:"+port+"/in_memory_db");
    // ...
}

理想情况下,我将在版本控制中签入一个postgres可执行文件,单元测试将使用该可执行文件.

Ideally I'll have a single postgres executable checked into the version control, which the unit test will use.

类似于HSQL的东西,但用于postgres.我该怎么办?

Something like HSQL, but for postgres. How can I do that?

我可以得到这样的Postgres版本吗?如何指示它不使用磁盘?

Were can I get such a Postgres version? How can I instruct it not to use the disk?

推荐答案

Postgres无法做到这一点.它不提供像HSQLDB或MySQL这样的进程内/内存引擎.

This is not possible with Postgres. It does not offer an in-process/in-memory engine like HSQLDB or MySQL.

如果要创建一个独立的环境,可以 将Postgres二进制文件放入SVN中(但它不仅仅是一个可执行文件).

If you want to create a self-contained environment you can put the Postgres binaries into SVN (but it's more than just a single executable).

您将需要运行 initdb 来设置测试数据库,然后您可以执行任何操作.这可以从批处理文件或通过使用Runtime.exec()完成.但是请注意,initdb并不是很快.您绝对不希望为每个测试都运行该程序.不过,在测试套件之前,您可能会逃避运行此操作.

You will need to run initdb to setup your test database before you can do anything with this. This can be done from a batch file or by using Runtime.exec(). But note that initdb is not something that is fast. You will definitely not want to run that for each test. You might get away running this before your test-suite though.

尽管可以这样做,但我建议安装专用的Postgres安装程序,您可以在其中简单地重新创建测试数据库,然后再运行测试.

However while this can be done, I'd recommend to have a dedicated Postgres installation where you simply recreate your test database before running your tests.

您可以使用模板数据库重新创建测试数据库,从而使创建数据库的速度非常快(每次测试运行,比运行initdb快很多)

You can re-create the test-database by using a template database which makes creating it quite fast (a lot faster than running initdb for each test run)

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

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