pg_dump 来自视图在 postgresql 10 中没有数据

pg_dump from view has no data in postgresql 10

pg_dump --table=export_view --data-only --column-inserts mydb > export_view.sql

pg_dump (PostgreSQL) 10.7 (Ubuntu 10.7-1.pgdg18.04+1)

Export specific rows from a PostgreSQL table as INSERT SQL script and the postgresql documentation (https://www.postgresql.org/docs/10/app-pgdump.html) suggest it is possible to pg_dump from a view with the --table flag. If I export from the table directly I get the expected result (ie, data is exported). If I select from the view in psql I get the expected result. However whether I create a view or a materialized view and then try and pg_dump, I get only the normal pg_dump headers and no data. A commenter (https://whosebug.com/users/2036135/poshest) 在上面的SO问题中似乎也遇到了同样的问题,没有给出解决方案。

如果我CREATE TABLE blah AS SELECT x, y, z FROM MYTABLE那么我可以很好地导出。如果我 CREATE VIEW blah AS SELECT x, y, z FROM MYTABLE 则导出为空。

我做错了什么?

视图不存储数据,它们提供数据的动态视图。当您在转储中包含视图时,您将只会获得视图定义。

, version 13 (and above?) - the current at the time this answer is written - indeed has clarification on what gets dumped:

As well as tables, this option can be used to dump the definition of matching views, materialized views, foreign tables, and sequences. It will not dump the contents of views or materialized views, and the contents of foreign tables will only be dumped if the corresponding foreign server is specified with --include-foreign-data.

(强调已添加)。

所以答案(对我自己)是:“你没有做错任何事,只是你错误地解释了 Postgres <=12 的文档。你想做的事是不可能的。”