Skip to content

Instantly share code, notes, and snippets.

@kevinke
Last active November 22, 2018 03:19
Show Gist options
  • Select an option

  • Save kevinke/8996dc420030c099f8cdfbb741345dda to your computer and use it in GitHub Desktop.

Select an option

Save kevinke/8996dc420030c099f8cdfbb741345dda to your computer and use it in GitHub Desktop.

Revisions

  1. kevinke revised this gist Nov 22, 2018. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion pg_dump_restore.sql
    Original file line number Diff line number Diff line change
    @@ -50,4 +50,8 @@ and restore with this:

    pg_restore 'pr_pro_db_test.dump' -F c -v -O -d ghl_pr_development -U 'postgres' -h localhost -p 15432

    don't need -c and -C if you restore to another database
    don't need -c and -C if you restore to another database
    create database with owner
    createdb -e -O <username> digital -h <host> -p <port> -U <root>
  2. kevinke renamed this gist Nov 22, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. kevinke revised this gist Sep 18, 2018. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion pg_dump_restore.md
    Original file line number Diff line number Diff line change
    @@ -44,4 +44,10 @@ WHERE

    -- don't kill the connections to other databases

    AND datname = 'database_name';
    AND datname = 'database_name';

    and restore with this:

    pg_restore 'pr_pro_db_test.dump' -F c -v -O -d ghl_pr_development -U 'postgres' -h localhost -p 15432

    don't need -c and -C if you restore to another database
  4. kevinke revised this gist Sep 18, 2018. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions pg_dump_restore.md
    Original file line number Diff line number Diff line change
    @@ -30,18 +30,18 @@ connection related param

    SELECT

    pg_terminate_backend(pid)
    pg_terminate_backend(pid)

    FROM

    pg_stat_activity
    pg_stat_activity

    WHERE

    -- don't kill my own connection!
    -- don't kill my own connection!

    pid <> pg_backend_pid()
    pid <> pg_backend_pid()

    -- don't kill the connections to other databases
    -- don't kill the connections to other databases

    AND datname = 'database_name';
    AND datname = 'database_name';
  5. kevinke revised this gist Sep 18, 2018. 1 changed file with 12 additions and 3 deletions.
    15 changes: 12 additions & 3 deletions pg_dump_restore.md
    Original file line number Diff line number Diff line change
    @@ -27,12 +27,21 @@ connection related param
    -d: dbname

    [//]: # kill active session before restore db dump
    SELECT

    SELECT

    pg_terminate_backend(pid)
    FROM

    FROM

    pg_stat_activity
    WHERE

    WHERE

    -- don't kill my own connection!

    pid <> pg_backend_pid()

    -- don't kill the connections to other databases

    AND datname = 'database_name';
  6. kevinke revised this gist Sep 17, 2018. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions pg_dump_restore.md
    Original file line number Diff line number Diff line change
    @@ -3,18 +3,27 @@ pg_dump -F c -v -O -c --if-exists -C -U 'root' -h '139.198.176.101' -p '15432' -
    dump related param

    -f [file path]: send output to the specified file

    -F [format]: c is for custom format

    -v: verbose

    -O: (an upcase 'Oscar') do not output commands to set ownership of objects

    -c: output commands to clean (drop) database objects prior to outputting the commands for creating them

    --if-exists: se conditional commands (i.e. add an IF EXISTS clause) when cleaning database objects.

    -C: begin the output with a command to create the database itself and reconnect to the created database.

    connection related param

    -h: host

    -p: port

    -U: user

    -d: dbname

    [//]: # kill active session before restore db dump
  7. kevinke created this gist Sep 17, 2018.
    29 changes: 29 additions & 0 deletions pg_dump_restore.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    pg_dump -F c -v -O -c --if-exists -C -U 'root' -h '139.198.176.101' -p '15432' -d 'qingcloud' -f 'pr_pro_db_test.dump'

    dump related param

    -f [file path]: send output to the specified file
    -F [format]: c is for custom format
    -v: verbose
    -O: (an upcase 'Oscar') do not output commands to set ownership of objects
    -c: output commands to clean (drop) database objects prior to outputting the commands for creating them
    --if-exists: se conditional commands (i.e. add an IF EXISTS clause) when cleaning database objects.
    -C: begin the output with a command to create the database itself and reconnect to the created database.

    connection related param

    -h: host
    -p: port
    -U: user
    -d: dbname

    [//]: # kill active session before restore db dump
    SELECT
    pg_terminate_backend(pid)
    FROM
    pg_stat_activity
    WHERE
    -- don't kill my own connection!
    pid <> pg_backend_pid()
    -- don't kill the connections to other databases
    AND datname = 'database_name';