PostgreSQL 转储临时表

发布于 2024-12-21 05:50:52 字数 558 浏览 4 评论 0原文

我使用以下查询在 PostgreSQL 数据库中创建了一个临时表

SELECT * INTO TEMP TABLE tempdata FROM data WHERE id=2004;

现在我想创建此临时表tempdata的备份。
因此,我使用以下命令行执行

"C:\Program Files\PostgreSQL\9.0\bin\pg_dump.exe" -F t -a -U my_admin -t tempdata myDB >"e:\mydump.backup"  

,收到一条消息:

pg_dump: No matching tables were found  

Is it possible to create a dump of temp table?
我做得正确吗?

PS:我也想恢复相同的内容。我不想使用任何额外的组件。

TIA。

I created a temp table in my PostgreSQL DB using the following query

SELECT * INTO TEMP TABLE tempdata FROM data WHERE id=2004;

Now I want to create a backup of this temp table tempdata.
So i use the following command line execution

"C:\Program Files\PostgreSQL\9.0\bin\pg_dump.exe" -F t -a -U my_admin -t tempdata myDB >"e:\mydump.backup"  

I get a message saying

pg_dump: No matching tables were found  

Is it possible to create a dump of temp tables?
Am I doing it correctly?

P.S. : I would also want to restore the same.I don't want to use any extra components.

TIA.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女七分熟 2024-12-28 05:50:52

我认为您无法将 pg_dump 用于该临时表。问题是 临时表仅存在于创建它们的会话中

PostgreSQL 要求每个会话为要使用的每个临时表发出自己的 CREATE TEMPORARY TABLE 命令。这允许不同的会话将相同的临时表名称用于不同的目的,而标准的方法限制给定临时表名称的所有实例具有相同的表结构。

因此,您可以在一个会话中创建临时表,但 pg_dump 将使用没有临时表的另一会话。

但是, COPY 应该可以工作:

COPY 在 PostgreSQL 表和标准文件系统文件之间移动数据。

但您可以将数据复制到标准输出或数据库服务器上的文件(需要超级用户访问权限):

带有文件名的 COPY 指示 PostgreSQL 服务器直接读取或写入文件。服务器必须可以访问该文件,并且必须从服务器的角度指定名称。
[...]
仅允许数据库超级用户对文件进行 COPY 命名,因为它允许读取或写入服务器有权访问的任何文件。

因此,使用 COPY 将临时表直接转储到文件可能不是一个选择。您可以复制到标准输出,但效果如何取决于您访问数据库的方式。

如果您不使用临时表,您可能会有更好的运气。当然,您必须管理唯一的表名称以避免与其他会话发生冲突,并且您必须小心确保非临时临时表在使用完毕后被删除。

I don't think you'll be able to use pg_dump for that temporary table. The problem is that temporary tables only exist within the session where they were created:

PostgreSQL instead requires each session to issue its own CREATE TEMPORARY TABLE command for each temporary table to be used. This allows different sessions to use the same temporary table name for different purposes, whereas the standard's approach constrains all instances of a given temporary table name to have the same table structure.

So you'd create the temporary table in one session but pg_dump would be using a different session that doesn't have your temporary table.

However, COPY should work:

COPY moves data between PostgreSQL tables and standard file-system files.

but you'll either be copying the data to the standard output or a file on the database server (which requires superuser access):

COPY with a file name instructs the PostgreSQL server to directly read from or write to a file. The file must be accessible to the server and the name must be specified from the viewpoint of the server.
[...]
COPY naming a file is only allowed to database superusers, since it allows reading or writing any file that the server has privileges to access.

So using COPY to dump the temporary table straight to a file might not be an option. You can COPY to the standard output though but how well that will work depends on how you're accessing the database.

You might have better luck if you didn't use temporary tables. You would, of course, have to manage unique table names to avoid conflicts with other sessions and you'd have to take care to ensure that your non-temporary temporary tables were dropped when you were done with them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文