导出使用PostgreSQL的Java项目

时间:2023-01-17 13:01:42

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中的计算机上的特定用户名和密码吗?

1 个解决方案

#1


0  

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. 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.

您是否希望两台计算机上的应用程序使用相同的数据库,以便在另一台计算机上看到更改?如果是这样,您需要配置应用程序的每个副本以连接到其中一台计算机上的同一数据库实例。这通常通过更改JDBC URL来完成。您需要在将成为数据库服务器的计算机上配置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. 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.

您是否希望每个安装都有一个单独的数据库实例,因此在一个实例上进行的更改对另一个实例没有影响,并且每个实例都以空白的空数据库(或者只包含查找表等静态内容的数据库)开始?如果是这样,您通常应该使用SQL脚本定义数据库,然后让应用程序在首次安装在计算机上时运行SQL脚本。如果你到目前为止已经手动定义了数据库,你可以使用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. You can automate this within your application / build system using tools like ProcessBuilder, or do it manually.

您是否希望计算机上的每个应用程序实例都具有独立的数据库,因此对其他实例进行的更改对其他计算机上的其他实例没有影响,但安装的起始状态是其他主机上的数据库中的内容?如果是这样,您需要使用pg_dump -Fc和pg_restore在应用程序旁边转储和重新加载数据库。您可以使用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.

没有通用的,罐装的方式来做到这一点。它需要您定义应用程序部署过程,可能会生成安装程序等。

#1


0  

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. 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.

您是否希望两台计算机上的应用程序使用相同的数据库,以便在另一台计算机上看到更改?如果是这样,您需要配置应用程序的每个副本以连接到其中一台计算机上的同一数据库实例。这通常通过更改JDBC URL来完成。您需要在将成为数据库服务器的计算机上配置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. 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.

您是否希望每个安装都有一个单独的数据库实例,因此在一个实例上进行的更改对另一个实例没有影响,并且每个实例都以空白的空数据库(或者只包含查找表等静态内容的数据库)开始?如果是这样,您通常应该使用SQL脚本定义数据库,然后让应用程序在首次安装在计算机上时运行SQL脚本。如果你到目前为止已经手动定义了数据库,你可以使用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. You can automate this within your application / build system using tools like ProcessBuilder, or do it manually.

您是否希望计算机上的每个应用程序实例都具有独立的数据库,因此对其他实例进行的更改对其他计算机上的其他实例没有影响,但安装的起始状态是其他主机上的数据库中的内容?如果是这样,您需要使用pg_dump -Fc和pg_restore在应用程序旁边转储和重新加载数据库。您可以使用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.

没有通用的,罐装的方式来做到这一点。它需要您定义应用程序部署过程,可能会生成安装程序等。