Export specific rows from a PostgreSQL table as INSERT SQL script

后端 未结 9 627
野趣味
野趣味 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:20

    I just knocked up a quick procedure to do this. It only works for a single row, so I create a temporary view that just selects the row I want, and then replace the pg_temp.temp_view with the actual table that I want to insert into.

    CREATE OR REPLACE FUNCTION dv_util.gen_insert_statement(IN p_schema text, IN p_table text)
      RETURNS text AS
    $BODY$
    DECLARE
        selquery text; 
        valquery text; 
        selvalue text; 
        colvalue text; 
        colrec record;
    BEGIN
    
        selquery := 'INSERT INTO ' ||  quote_ident(p_schema) || '.' || quote_ident(p_table);
    
        selquery := selquery || '(';
    
        valquery := ' VALUES (';
        FOR colrec IN SELECT table_schema, table_name, column_name, data_type
                      FROM information_schema.columns 
                      WHERE table_name = p_table and table_schema = p_schema 
                      ORDER BY ordinal_position 
        LOOP
          selquery := selquery || quote_ident(colrec.column_name) || ',';
    
          selvalue := 
            'SELECT CASE WHEN ' || quote_ident(colrec.column_name) || ' IS NULL' || 
                       ' THEN ''NULL''' || 
                       ' ELSE '''' || quote_literal('|| quote_ident(colrec.column_name) || ')::text || ''''' || 
                       ' END' || 
            ' FROM '||quote_ident(p_schema)||'.'||quote_ident(p_table);
          EXECUTE selvalue INTO colvalue;
          valquery := valquery || colvalue || ',';
        END LOOP;
        -- Replace the last , with a )
        selquery := substring(selquery,1,length(selquery)-1) || ')';
        valquery := substring(valquery,1,length(valquery)-1) || ')';
    
        selquery := selquery || valquery;
    
    RETURN selquery;
    END
    $BODY$
      LANGUAGE plpgsql VOLATILE;
    

    Invoked thus:

    SELECT distinct dv_util.gen_insert_statement('pg_temp_' || sess_id::text,'my_data') 
    from pg_stat_activity 
    where procpid = pg_backend_pid()
    

    I haven't tested this against injection attacks, please let me know if the quote_literal call isn't sufficient for that.

    Also it only works for columns that can be simply cast to ::text and back again.

    Also this is for Greenplum but I can't think of a reason why it wouldn't work on Postgres, CMIIW.

    0 讨论(0)
  • 2020-11-30 17:27

    have u tried in pgadmin executing query with " EXECUTE QUERY WRITE RESULT TO FILE " option

    its only export the data, else try like

    pg_dump -t view_name DB_name > db.sql
    

    -t option used for ==> Dump only tables (or views or sequences) matching table, refer

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题