Export specific rows from a PostgreSQL table as INSERT SQL script

后端 未结 9 742
野趣味
野趣味 2020-11-30 17:01

I have a database schema named: nyummy and a table named cimory:

create table nyummy.cimory (
  id numeric(10,0) not null,
  name c         


        
9条回答
  •  一向
    一向 (楼主)
    2020-11-30 17:29

    Create a table with the set you want to export and then use the command line utility pg_dump to export to a file:

    create table export_table as 
    select id, name, city
    from nyummy.cimory
    where city = 'tokyo'
    
    $ pg_dump --table=export_table --data-only --column-inserts my_database > data.sql
    

    --column-inserts will dump as insert commands with column names.

    --data-only do not dump schema.

    As commented below, creating a view in instead of a table will obviate the table creation whenever a new export is necessary.

提交回复
热议问题