如何在PostgreSQL中导出数据库的模式?

pro*_*mer 94 postgresql schema export

我的电脑坏了但幸运的是我备份了文件夹C:\ Program Files\PostgreSQL.

现在我在一台新计算机上工作,我想导入以前存储在外部磁盘中的Postgres数据库.

我想导出位于备份文件夹中的特定数据库的架构.

该文件PostgreSQL\8.3\data\global\pg_database包含有关数据库及其OID的信息; 例如:

"db1" 20012
"db2" 23456
Run Code Online (Sandbox Code Playgroud)

我想导出"db1"的架构.

文件夹中有一个名为"20012"的文件夹"PostgreSQL\8.3\data\base\20012" ,其中包含大量文件[500个文件].

有没有办法导出该数据库的架构?

请注意,所有Postgresql数据库文件都位于外部硬盘中,我想将该数据库的模式导出到SQL文件中,获取该文件,运行它并在本地创建相同的精确数据库.

Ane*_*new 125

你应该看看pg_dump:

pg_dump -s databasename
Run Code Online (Sandbox Code Playgroud)

将仅将模式转储为stdout为.sql.

对于Windows,你可能想打电话pg_dump.exe.我无法访问Windows机器,但我很确定从内存中可以看出这个命令.看看帮助是否也适用于您.

  • @Anew谢谢它的作品.`pg_dump -s databasename> schema.sql`将结果输出到`schema.sql`文件中 (11认同)
  • 对我来说,我发现 `pg_dump $PSQL_CONNECT_STRING -s > myschema.sql` 很有用,我在其中设置了 `PSQL_CONNECT_STRING`,如 https://www.postgresql.org/docs/9.2/libpq-connect.html#LIBPQ- 中所述连接字符串。 (2认同)

小智 35

Linux中你可以这样做

pg_dump -U postgres -s postgres > exportFile.dmp
Run Code Online (Sandbox Code Playgroud)

也许它也适用于Windows,如果没有尝试与pg_dump.exe相同

pg_dump.exe -U postgres -s postgres > exportFile.dmp
Run Code Online (Sandbox Code Playgroud)

  • 在Windows(和Mac)FYI中的工作方式相同。 (2认同)

Jam*_*hin 26

我正在运行Postgres 9.6,我必须导出特定的模式和数据.

我使用以下命令:

pg_dump.exe -U username -d databasename -n schemaname > C:\mylocation\mydumpfilename.dmp
Run Code Online (Sandbox Code Playgroud)

如果只想要没有数据的模式,请使用开关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>.
Run Code Online (Sandbox Code Playgroud)


小智 20

pg_dump -d <databasename> -h <hostname> -p <port> -n <schemaname> -f <location of the dump file>
Run Code Online (Sandbox Code Playgroud)

请注意,您有足够的权限访问该架构。如果您想以特定用户的身份进行备份,请在该命令中添加用户名-U

  • pg_dump -s &lt;数据库名称&gt; -h &lt;主机名&gt; -p &lt;端口&gt; -n &lt;架构名称&gt; -f &lt;转储文件的位置&gt; 将标志从 -d 更改为 -s 以仅获取架构。正如其他人也提到的那样,这只是为了改进这一点。 (2认同)

Lal*_*gad 8

对于Linux:(数据除外)

  • pg_dump -s -t tablename databasename > dump.sql (对于数据库中的特定表)

  • pg_dump -s databasename > dump.sql (对于整个数据库)


aro*_*rod 5

如果只需要创建表,则可以 pg_dump -s databasename | awk 'RS="";/CREATE TABLE[^;]*;/'