如果可能,如何嵌入 PostgreSQL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/60285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If possible how can one embed PostgreSQL?
提问by pc1oad1etter
If it's possible, I'm interested in being able to embed a PostgreSQL database, similar to sqllite. I've read that it's not possible. I'm no database expert though, so I want to hear from you.
如果可能的话,我有兴趣能够嵌入 PostgreSQL 数据库,类似于sqllite。我读到这是不可能的。我不是数据库专家,所以我想听听你的意见。
Essentially I want PostgreSQL without all the configuration and installation. If it's possible, tell me how.
本质上,我想要没有所有配置和安装的 PostgreSQL。如果可能的话,告诉我怎么做。
采纳答案by Crag
Unless you do a major rewrite of code, it is not possible to run Postgres "embedded". Either run it as a separate process or use something else. SQLite is an excellent choice. But there are others. MySQL has an embedded version. See it at http://mysql.com/oem/. Also several java choices, and Mac has Core Data you can write too. Hell, you can even use FoxPro. What OS you on and what services you need from the database?
除非您对代码进行重大重写,否则不可能“嵌入”运行 Postgres。要么将其作为单独的进程运行,要么使用其他程序。SQLite 是一个很好的选择。但还有其他人。MySQL 有一个嵌入式版本。在http://mysql.com/oem/ 上查看。还有几个 java 选择,Mac 也有你可以编写的 Core Data。见鬼,您甚至可以使用 FoxPro。您使用的是什么操作系统以及您需要从数据库获得哪些服务?
回答by brcha
Run postgresql in a background process.
在后台进程中运行 postgresql。
Start a separate thread in your application that would start a postgresql server in local mode either by binding it to localhost with some random free port or by using sockets (does windows support sockets?). That should be fairly easy, something like:
在您的应用程序中启动一个单独的线程,该线程将在本地模式下启动 postgresql 服务器,方法是将其绑定到具有一些随机空闲端口的 localhost 或使用套接字(Windows 是否支持套接字?)。这应该相当容易,例如:
system("C:\Program Files\MyApplication\pgsql\postgres.exe -D C:\Documents and Settings\User\Local Settings\MyApplication\database -h 127.0.0.1 -p 12345");
system("C:\Program Files\MyApplication\pgsql\postgres.exe -DC:\Documents and Settings\User\Local Settings\MyApplication\database -h 127.0.0.1 -p 12345");
and then just connect to 127.0.0.1:12345.
然后只需连接到 127.0.0.1:12345。
When your application quits, you can always send a SIGTERM to your thread and then wait a few seconds for postgresql to quit (ie join the thread).
当您的应用程序退出时,您始终可以向您的线程发送一个 SIGTERM,然后等待几秒钟让 postgresql 退出(即加入线程)。
PS: You can also use pg_ctlto control your "embedded" database, even without threads, just do a "pg_ctl start" (with appropriate options) when starting the application and "pg_ctl stop" when quitting it.
PS:您也可以使用pg_ctl来控制您的“嵌入式”数据库,即使没有线程,只需在启动应用程序时执行“pg_ctl start”(带有适当的选项),并在退出应用程序时执行“pg_ctl stop”。
回答by Brian R. Bondy
You cannot embed it, nor should you try.
您不能嵌入它,也不应该尝试。
For embedding you should use sqliteas you mentioned or firebird rdbms.
对于嵌入,您应该使用您提到的sqlite或firebird rdbms。
回答by Crag
You can't embed it as a in process type thing like sqlite etc, but you can easily embed it into your application setup using Inno setup at http://www.innosetup.org. Search their mailing list archive and you will find someone did most of the work for you and all you have to to is grab the zipped distro and you can easily have postgresql installed when the user installs your app. You can then use the pg_hba.conf file to restrict the server to local host only. Not a true embedded DB, but it would work.
您不能将它嵌入为诸如 sqlite 等的进程中类型的东西,但您可以使用http://www.innosetup.org 上的Inno setup 轻松地将它嵌入到您的应用程序设置中。搜索他们的邮件列表存档,您会发现有人为您完成了大部分工作,您所要做的就是获取压缩的发行版,并且您可以在用户安装您的应用程序时轻松安装 postgresql。然后您可以使用 pg_hba.conf 文件将服务器限制为仅本地主机。不是真正的嵌入式数据库,但它可以工作。
回答by Karel Vervaeke
HSQLDB (http://hsqldb.org/) is another db which is easily embedded. Requires Java, but is an excellent and often-used choice for Java applications.
HSQLDB (http://hsqldb.org/) 是另一个易于嵌入的数据库。需要 Java,但对于 Java 应用程序来说,它是一个出色且经常使用的选择。
回答by John Millikin
PostgreSQL is intended to run as a stand-alone server; it's probably possible to embed it if you hack at it hard and long enough, but it would be much easier to just run it as intended in a separate process.
PostgreSQL 旨在作为独立服务器运行;如果您对其进行了足够的努力和长时间的破解,则可能可以嵌入它,但是在单独的过程中按预期运行它会容易得多。
回答by John Millikin
Anyone tried on Mac OS X:
任何人在 Mac OS X 上尝试过:
http://pagesperso-orange.fr/bruno.gaufier/xhtml/prod_postgresql.xhtml
http://pagesperso-orange.fr/bruno.gaufier/xhtml/prod_postgresql.xhtml
http://www.macosxguru.net/article.php?story=20041119135924825
http://www.macosxguru.net/article.php?story=20041119135924825
(Of course sqlite would be my embedded db of choice as well)
(当然,sqlite 也是我选择的嵌入式数据库)

