Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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://stackoverflow.com/users/2036135/poshest) also appears to have faced the same issue in the above SO question, with no solution given.

If I CREATE TABLE blah AS SELECT x, y, z FROM MYTABLE then I can export fine. If I CREATE VIEW blah AS SELECT x, y, z FROM MYTABLE then the export is empty.

What am I doing wrong?

like image 214
AntonOfTheWoods Avatar asked Feb 25 '19 07:02

AntonOfTheWoods


People also ask

Where is my pg_dump file?

pg_dump, pg_dump_all, pg_restore are all located in the bin folder of the PostgreSQL install and PgAdmin III install.

Does pg_dump backup data?

pg_dump is a utility for backing up a PostgreSQL database. It makes consistent backups even if the database is being used concurrently. pg_dump does not block other users accessing the database (readers or writers). pg_dump only dumps a single database.

Does pg_dump delete database?

Note: pg_dump & pg_restore don't fully clear the entire database. DROP SCHEMA public CASCADE; CREATE SCHEMA public; If you are using PostgreSQL 9.3 or greater, you may also need to restore the default grants.


1 Answers

As @bugmenot points out, 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.

(emphasis added).

So the answer (to myself) is: "You are not doing anything wrong, except that you incorrectly interpreted the documentation for Postgres <=12. What you want to do is not possible."

like image 84
AntonOfTheWoods Avatar answered Oct 08 '22 19:10

AntonOfTheWoods