postgresql 为用户将数据库无缝打包到应用程序中

v1l68za4  于 2023-08-04  发布在  PostgreSQL
关注(0)|答案(3)|浏览(203)

我想创建一个使用关系数据库的桌面应用程序(比如postgres --假设我最好的情况是在这个应用程序中使用postgres)。
我希望用户不知道数据库。目前,我必须将postgres安装到我的本地计算机上,并让我的应用程序与之通信。
我在用Go。
我该如何避免这种情况?

4xy9mtcn

4xy9mtcn1#

您正在寻找一个 * 嵌入式数据库 *。
This isn't an ideal job for PostgreSQL,但you can use it that way with a bit of care
请不要捆绑安装程序并在无人值守的情况下运行。当用户后来去安装PostgreSQL时,当他们看到它已经在他们的计算机上,但他们不知道为什么,谁安装了它,或者密码是什么时,他们会感到非常困惑。
相反,initdb在应用程序的%APPDATA%中或(对于多用户共享)在%PROGRAMDATA%中创建一个新的datadir。设置自定义端口(不要使用默认的5432)。使用pg_ctl register创建一个新服务,作为NETWORKSERVICE运行,或者使用pg_ctl按需启动/停止。这样你就不会妨碍任何现有的PostgreSQL安装或新的安装,并为你的应用程序提供一个私有的PostgreSQL。
但是,请为用户提供为现有PostgreSQL提供连接字符串的选项。如果应用程序坚持使用自己的嵌入式副本,而你不希望他们这样做,这是一个痛苦。
不过,通常最好考虑使用SQLite、H2、Derby、Firebird或其他嵌入式数据库之一。

kuuvgm7e

kuuvgm7e2#

简短的版本,你真的不能,你最好的选择是使用SQLite或类似的。
如果你真的想,你可以为你的数据库创建多个无人值守的安装程序,针对你想要的每个平台,将它嵌入到你的应用程序中,并在第一次运行时安装它。
现在,这只是丑陋和大多数用户(包括我自己)将彻底永远不会使用它。
您可以随时提到您的软件依赖于X和Y,并提供有关如何手动安装依赖项的信息。

n6lpvg4x

n6lpvg4x3#

这封电子邮件线程闪耀一些光到适当的方式来做它在古老的窗口。很可能有类似的帖子,其中他们更详细。https://www.postgresql.org/message-id/4D2FFF07.6060409@aimproductions.be

Hi,


On 14/01/2011 7:35, Craig Ringer wrote:
> On 01/14/2011 06:26 AM, Jensen Somers wrote:
>
>> I know that some applications (Poker Tracker 3 is the first one that
>> comes to my mind)
>
> It's not a good example, either, as demonstrated by the number of 
> questions that pop up about it on this list, and the incredibly 
> ancient versions of Pg that they bundle.
>
>> install PostgreSQL during their installation process
>> too and setup the initial database. I sort of figured out how to do
>> this, but I don't know how to deal with an already existing installation
>> during setup.
>
> I take it you're talking about doing a silent install using the 
> postgresql exe installer, by invoking it as part of your own app's 
> installer?
>
> Personally, that's not how I'd do it if I were bundling Pg in my 
> (Windows) app, because as you mentioned it may interfere with any 
> existing or future Pg install the user wants to do manually. It'll 
> also show up separately in add/remove programs, which I think is 
> undesirable when it's just being installed as a component of your app.
>
> If my app required Pg, I'd probably bundle the Pg installation tree in 
> my installer and copy it into my program's install directory. I'd then 
> create a non-login user account named after my application (NOT 
> "postgres"), set up the service (again named for my application), and 
> invoke initdb to create the database under that service account. I'd 
> generate a postgresql.conf with a semi-random fairly high port number 
> that wasn't already in use on the target machine, to avoid conflicting 
> with the commonly used postgresql port. All this can be done using the 
> scripting languages provided by most installers, or via simple Windows 
> command line tools like "net.exe" and friends. If your installer is 
> particularly limited, you can always write and bundle a simple helper 
> program for it to invoke to do the work. My uninstaller would prompt 
> the user to ask if they wanted to remove data as well as the program; 
> if they said yes I'd remove the datadir and the user account I'd 
> created during installation.
>
> This way, your instance of PostgreSQL is private to your app and 
> doesn't conflict with anything the user might want to do. You can 
> upgrade it when you upgrade your app, provide backup facilities for it 
> in your app, etc etc without the user having to care what's behind the 
> scenes.
>
>
> If that wasn't viable, the only other option  I'd consider would be 
> providing a postgresql installer and asking the user to install it if 
> they didn't already have it installed. I'd then prompt for the 
> database host, port, username, password and database name to connect 
> to, and would just use what was provided to me. This is almost 
> certainly how it should be done on UNIX/Linux platforms.
>
> -- 
> Craig Ringer


Bundling it as part of my application is even better. I didn't knew if 
that would be possible, but it would solve some of the issues. Mainly 
data protection. The data that needs to be stored should not be altered 
by users. If they have access to the database via a root password, which 
would be the case when using the installer or an existing server they 
can manipulate the data. Implementing your suggested solution would 
prevent all that, which makes it a perfect solution.


Thanks, I know what I'll be doing today!


 - Jensen

字符串

相关问题