programing tip

PostgreSQL에서 데이터베이스 스키마를 내보내려면 어떻게해야합니까?

itbloger 2020. 9. 1. 07:18
반응형

PostgreSQL에서 데이터베이스 스키마를 내보내려면 어떻게해야합니까?


내 컴퓨터가 고장 났지만 다행히 C : \ Program Files \ PostgreSQL 폴더를 백업했습니다.

이제 새 컴퓨터에서 작업 중이며 외부 디스크에 저장된 이전 Postgres 데이터베이스를 가져오고 싶습니다.

백업 폴더에있는 특정 데이터베이스의 스키마를 내보내고 싶습니다.

이 파일 PostgreSQL\8.3\data\global\pg_database에는 데이터베이스 및 해당 OID에 대한 정보가 포함되어 있습니다. 예를 들면 :

"db1" 20012
"db2" 23456

"db1"의 스키마를 내보내고 싶습니다.

"PostgreSQL\8.3\data\base\20012"많은 파일 [500 개 파일]이 포함 된 폴더에 "20012"라는 이름의 폴더 가 있습니다.

해당 데이터베이스의 스키마를 내보내는 방법이 있습니까?

모든 Postgresql 데이터베이스 파일은 외부 하드 디스크에 있으며 해당 데이터베이스의 스키마를 SQL 파일로 내보내고 해당 파일을 가져 와서 실행하고 동일한 정확한 데이터베이스를 로컬로 만들고 싶습니다.


다음을 살펴보아야합니다 pg_dump.

pg_dump -s databasename

스키마 만 stdout에 .sql로 덤프합니다.

Windows의 경우 pg_dump.exe. 나는 Windows 시스템에 액세스 할 수 없지만 메모리에서 명령 인 것을 확신합니다. 도움이 당신에게도 효과가 있는지 확인하십시오.


Linux 에서는 다음과 같이 할 수 있습니다.

pg_dump -U postgres -s postgres > exportFile.dmp

pg_dump.exe 와 동일하게 시도하지 않으면 Windows 에서도 작동 할 수 있습니다.

pg_dump.exe -U postgres -s postgres > exportFile.dmp

데이터와 함께 특정 스키마를 내 보내야하는 Postgres 9.6을 실행 중입니다.

다음 명령을 사용했습니다.

pg_dump.exe -U username -d databasename -n schemaname > C:\mylocation\mydumpfilename.dmp

데이터없이 스키마 만 원하는 경우 s대신 스위치 사용하십시오.n

다음은 pg_dump 스위치 목록입니다.

C:\Program Files\PostgreSQL\9.6\bin>pg_dump --help
pg_dump dumps a database as a text file or to other formats.

Usage:
  pg_dump [OPTION]... [DBNAME]

General options:
  -f, --file=FILENAME          output file or directory name
  -F, --format=c|d|t|p         output file format (custom, directory, tar,
                               plain text (default))
  -j, --jobs=NUM               use this many parallel jobs to dump
  -v, --verbose                verbose mode
  -V, --version                output version information, then exit
  -Z, --compress=0-9           compression level for compressed formats
  --lock-wait-timeout=TIMEOUT  fail after waiting TIMEOUT for a table lock
  -?, --help                   show this help, then exit

Options controlling the output content:
  -a, --data-only              dump only the data, not the schema
  -b, --blobs                  include large objects in dump
  -c, --clean                  clean (drop) database objects before recreating
  -C, --create                 include commands to create database in dump
  -E, --encoding=ENCODING      dump the data in encoding ENCODING
  -n, --schema=SCHEMA          dump the named schema(s) only
  -N, --exclude-schema=SCHEMA  do NOT dump the named schema(s)
  -o, --oids                   include OIDs in dump
  -O, --no-owner               skip restoration of object ownership in
                               plain-text format
  -s, --schema-only            dump only the schema, no data
  -S, --superuser=NAME         superuser user name to use in plain-text format
  -t, --table=TABLE            dump the named table(s) only
  -T, --exclude-table=TABLE    do NOT dump the named table(s)
  -x, --no-privileges          do not dump privileges (grant/revoke)
  --binary-upgrade             for use by upgrade utilities only
  --column-inserts             dump data as INSERT commands with column names
  --disable-dollar-quoting     disable dollar quoting, use SQL standard quoting
  --disable-triggers           disable triggers during data-only restore
  --enable-row-security        enable row security (dump only content user has
                               access to)
  --exclude-table-data=TABLE   do NOT dump data for the named table(s)
  --if-exists                  use IF EXISTS when dropping objects
  --inserts                    dump data as INSERT commands, rather than COPY
  --no-security-labels         do not dump security label assignments
  --no-synchronized-snapshots  do not use synchronized snapshots in parallel jobs
  --no-tablespaces             do not dump tablespace assignments
  --no-unlogged-table-data     do not dump unlogged table data
  --quote-all-identifiers      quote all identifiers, even if not key words
  --section=SECTION            dump named section (pre-data, data, or post-data)
  --serializable-deferrable    wait until the dump can run without anomalies
  --snapshot=SNAPSHOT          use given snapshot for the dump
  --strict-names               require table and/or schema include patterns to
                               match at least one entity each
  --use-set-session-authorization
                               use SET SESSION AUTHORIZATION commands instead of
                               ALTER OWNER commands to set ownership

Connection options:
  -d, --dbname=DBNAME      database to dump
  -h, --host=HOSTNAME      database server host or socket directory
  -p, --port=PORT          database server port number
  -U, --username=NAME      connect as specified database user
  -w, --no-password        never prompt for password
  -W, --password           force password prompt (should happen automatically)
  --role=ROLENAME          do SET ROLE before dump

If no database name is supplied, then the PGDATABASE environment
variable value is used.

Report bugs to <pgsql-bugs@postgresql.org>.

테이블 만들기 만 원하는 경우 다음을 수행 할 수 있습니다. pg_dump -s databasename | awk 'RS="";/CREATE TABLE[^;]*;/'


pg_dump -d <databasename> -h <hostname> -p <port> -n <schemaname> -f <location of the dump file>

해당 스키마에 액세스 할 수있는 충분한 권한이 있습니다. 특정 사용자로 백업하려면 해당 명령 앞에 사용자 이름을 추가하십시오.-U


새 postgresql 서버를 설정하고 데이터 폴더를 외부 디스크의 파일로 바꿉니다.

그러면 해당 postgresql 서버를 시작하고 pg_dump를 사용하여 데이터를 검색 할 수 있습니다 (설명 된대로 스키마 전용의 경우 pg_dump -s).


다음과 같은 것을 사용해야합니다. pg_dump --schema=your_schema_name db1자세한 내용은 여기 를 참조 하세요.

참고 URL : https://stackoverflow.com/questions/14486241/how-can-i-export-the-schema-of-a-database-in-postgresql

반응형