简体   繁体   English

导出使用PostgreSQL的Java项目

[英]Exporting a Java project that uses PostgreSQL

I couldn't find an answer to this question. 我找不到这个问题的答案。 How can I export a Java project that makes use of a PostgreSQL database? 如何导出使用PostgreSQL数据库的Java项目?

I want to use the same database on another computer. 我想在另一台计算机上使用相同的数据库。 Do I need to export the database itself with the project? 我是否需要将数据库本身与项目一起导出? How can this be done? 如何才能做到这一点?

What should the connection URL be, so that the database is accessible on another computer? 连接URL应该是什么,以便可以在另一台计算机上访问该数据库?

I'm using JDBC, and I'm on Windows. 我正在使用JDBC,并且在Windows上。

Thanks in advance. 提前致谢。

EDIT: Wouldn't I also need to dynamically retrieve the username and password on the other computer, instead of using the specific username and password I have on my computer in PostgreSQL? 编辑:我是否还需要动态检索另一台计算机上的用户名和密码,而不是使用我在PostgreSQL中的计算机上拥有的特定用户名和密码?

It really depends on what you want to achieve. 这实际上取决于您要实现的目标。

Shared database between hosts 主机之间的共享数据库

Do you want the application on both computers to use the same database, so that changes made by one are seen on the other? 您是否希望两台计算机上的应用程序使用相同的数据库,以便在一台计算机上看到所做的更改? If so, you need to configure each copy of the application to connect to the same database instance on one of the machines. 如果是这样,则需要配置应用程序的每个副本以连接到其中一台计算机上的同一数据库实例。 This is usually done by changing the JDBC URL. 通常,这是通过更改JDBC URL来完成的。 You'll need to configure PostgreSQL on the machine that'll be the database server so it allows connections from the other hosts, ensure they can talk to each other over TCP/IP, etc. 您需要在将成为数据库服务器的机器上配置PostgreSQL,以便它允许来自其他主机的连接,确保它们可以通过TCP / IP相互通信,等等。

Fresh DB on each host 每个主机上都有新数据库

Do you want each install to have a separate instance of the database, so changes made on one have no effect on the other, and where each instance starts out with a blank, empty database (or one with only static contents like lookup tables)? 您是否希望每个安装都具有一个单独的数据库实例,以便对一个实例所做的更改对另一个实例无效,并且每个实例以空白的空数据库(或仅包含静态内容(例如查找表))开头? If so, you should generally define the database using SQL scripts, then have the application run the SQL scripts when first installed on a machine. 如果是这样,通常应该使用SQL脚本定义数据库,然后让应用程序在首次安装在计算机上时运行SQL脚本。 If you've defined the database by hand so far, you can use pg_dump to create a SQL script that you can use as the basis for this, but I really advise you to look into tools like Liquibase for schema management instead. 如果到目前为止,您已经手工定义了数据库,则可以使用pg_dump创建一个SQL脚本,以此作为基础,但是我确实建议您研究诸如Liquibase之类的工具来进行模式管理。

"Fork" current state “叉”的当前状态

Do you want each instance of the application on a machine to have an independent database, so changes made on one have no effect on other instances on other machines, but where the starting state of an install is what was in the database on the other host? 您是否希望一台计算机上的应用程序的每个实例都具有一个独立的数据库,所以对一个实例所做的更改不会影响其他计算机上的其他实例,但是安装的开始状态是另一台主机上数据库中的安装状态? If so, you need to dump and reload the database alongside the application, using pg_dump -Fc and pg_restore . 如果是这样,则需要使用pg_dump -Fcpg_restore 转储并与应用程序一起重新加载数据库。 You can automate this within your application / build system using tools like ProcessBuilder , or do it manually. 您可以使用ProcessBuilder工具在应用程序/构建系统中自动执行此操作,也可以手动执行此操作。

There's no generic, canned way to do this. 没有通用的固定方式可以做到这一点。 It'll require you to define an application deployment procedure, maybe produce an installer, etc. 它将要求您定义应用程序部署过程,可能会产生安装程序等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM