Answers:
*基本示例:
我在窗口中创建“ dump.bat”和“ restore.bat”文件以转储/还原
1 /备份:
"C:\Program Files\PostgreSQL\9.1\bin\pg_dump.exe" --host localhost --port 5432 --username "postgres" --role "postgres" --format plain --encoding UTF8 --schema-only --file "dump_resul.sql" --schema "name_schema_B" "name_database_A"
结果:
-- PostgreSQL database dump
-- Dumped from database version 9.1.4
-- Dumped by pg_dump version 9.1.4
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET search_path = public, pg_catalog;
CREATE TABLE abtb_temp (
id bigint NOT NULL,
app_code character varying(100)
); ....
*注意:一些重要选项:
--data-only, --format=format (ex: format=tar -> if you have a big database), --schema-only, --table=table (ex: --table=schema_name.table_name) ...
2 /恢复:
"C:\Program Files\PostgreSQL\9.1\bin\pg_restore.exe" --host localhost --port 5432 --username "postgres" --dbname "any_database" --no-password --no-owner --no-privileges --schema name_schema_B --verbose "C:\dump_resul.sql"
(**)
(**)实际上,如果您的格式文件是* .sql,则可以使用pgAdmin(或psql)进行还原。您应该使用pg_restore还原文件.tar( .bakup ...)
--schema-only
,数据必须位于转储中,否则您可能没有权限选择表内容。
我写了一个脚本,使这个琐碎。轻松将SQL函数和表模式导出到单个*.sql
文件中。使管理,备份和源代码管理变得容易:
#!/bin/sh
# Export all table schemas and functions into individual *.sql files for easy management and source control.
TEMP_WORK_PATH="/tmp/postgres"
BACKUP_ROOT_PATH="/vagrant/backup" # This is where your *.sql files will be exported at
DATABASE_NAME="myapp_db"
DATABASE_TABLES_PREFIX="myapp_"
POSTGRES_USER="postgres"
POSTGRES_PASSWORD="postgres"
[ -d $TEMP_WORK_PATH ] || mkdir -p $TEMP_WORK_PATH
rm -rf $TEMP_WORK_PATH/*
[ -d $BACKUP_ROOT_PATH ] || mkdir -p $BACKUP_ROOT_PATH
rm -rf $BACKUP_ROOT_PATH/*
mkdir $BACKUP_ROOT_PATH/tables
mkdir $BACKUP_ROOT_PATH/routines
export PGPASSWORD=$POSTGRES_PASSWORD
cd $TEMP_WORK_PATH
# Get all table schemas and write to individual files
echo "Export table schemas..."
for table in $(psql -U $POSTGRES_USER -d $DATABASE_NAME -t -c "Select table_name From information_schema.tables Where table_type='BASE TABLE' and table_name like '$DATABASE_TABLES_PREFIX%'");
do pg_dump -st $table -U $POSTGRES_USER $DATABASE_NAME > $BACKUP_ROOT_PATH/tables/$table.sql;
done;
# Get all functions in db and output to one file
echo "Getting stored functions..."
psql -U $POSTGRES_USER -At $DATABASE_NAME > $TEMP_WORK_PATH/db_functions.sql <<EOF
SELECT pg_get_functiondef(f.oid)
FROM pg_catalog.pg_proc f
INNER JOIN pg_catalog.pg_namespace n ON (f.pronamespace = n.oid)
WHERE n.nspname = 'public';
EOF
# Split function file into separate files per function
echo "Exporting stored functions..."
csplit -f function -b '%d.sql' db_functions.sql '/FUNCTION/' '{*}'
# Rename the function files to match the function name in the file
# then place the files into the target backup directory
counter=1
while [ -f $TEMP_WORK_PATH/function$counter.sql ]
do
name=$(head -1 function$counter.sql | awk {'print $5'})
name=$(echo $name | cut -d "." --f 2 | cut -d "(" --f 1)
mv function$counter.sql $BACKUP_ROOT_PATH/routines/$name.sql
counter=$((counter+1))
done
echo "done"
https://gist.github.com/dantheman213/aff70ee42a11f2d1fa46983878cd62e1
-b
选项与Blob转储。注意,-b
不仅为单个架构添加了所有Blob,还添加了所有Blob。