Using pg_dump to only get insert statements from one table within database

Using pg_dump to only get insert statements from one table within database

Asked on January 8, 2019 in PostgreSQL.
Add Comment


  • 2 Answer(s)

    User need to DUMP your inserts into an .sql file:

    cd to the location which you want to .sql file to be located

    pg_dump --column-inserts --data-only --table=<table> <database> > my_dump.sql
    
    

    NOTE: the > my_dump.sql command. This will put everything into a sql file named my_dump

    Answered on January 8, 2019.
    Add Comment

    This query include  your script :

     

    #!/bin/bash
    set -o xtrace # remove me after debug
    TABLE=charge_unit
    DB_NAME=prod_sit_entities_db
    BASE_DIR=/var/backups/someDir
    LOCATION="${BASE_DIR}/myApp_$(date +%Y%m%d_%H%M%S)"
    FNAME="${LOCATION}_${DB_NAME}_${TABLE}.sql"
    # Create backups directory if not exists
    if [[ ! -e $BASE_DIR ]];then
    | mkdir $BASE_DIR
    | chown -R postgres:postgres $BASE_DIR
    fi
    sudo -H -u postgres pg_dump --column-inserts --data-only --table=$TABLE $DB_NAME > $FNAME
    sudo gzip $FNAME
    

     

    Answered on January 8, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.