...
此处为了影响最小,写了一个分离程序,将导出的部分拆分成了main和barnard两个schema
代码块 | ||
---|---|---|
| ||
# How to run
# python postgresql-dump-extractor.py \
# -s barnard \
# -i 576184071779_dump_20240923.sql\
# -o 576184071779_dump_20240923_barnard.sql
import argparse
import re
def extract_copy_statements(input_file, output_file, schema):
with open(input_file, 'r') as infile, open(output_file, 'w') as outfile:
copy_block = []
in_copy_block = False
copy_pattern = re.compile(rf'^COPY {re.escape(schema)}\.(\w+)')
for line in infile:
if not in_copy_block:
if copy_pattern.match(line):
in_copy_block = True
copy_block = [line]
else:
copy_block.append(line)
if line.strip() == '\.':
outfile.writelines(copy_block)
outfile.write('\n')
in_copy_block = False
copy_block = []
def main():
parser = argparse.ArgumentParser(description='Extract COPY statements from PostgreSQL dump file.')
parser.add_argument('-i', '--input', required=True, help='Input dump file name')
parser.add_argument('-o', '--output', required=True, help='Output file name')
parser.add_argument('-s', '--schema', required=True, help='Schema name')
args = parser.parse_args()
extract_copy_statements(args.input, args.output, args.schema)
print(f"Extraction complete. Output written to {args.output}")
if __name__ == "__main__":
main() |
运行以下命令可以将其数据分离出来,可以通过psql命令导入运行以下命令可以将其数据分离出来,注意每次都要指定 schema 命令,之后可以通过psql命令导入分离出来的sql脚本。
代码块 | ||
---|---|---|
| ||
python postgresql-dump-extractor.py \ -s barnard \ -i 576184071779_dump_20240923.sql\ -o 576184071779_dump_20240923_barnard.sql |
...