【问题标题】:How can I export the schema of a database in PostgreSQL?如何在 PostgreSQL 中导出数据库的模式?
【发布时间】:2013-01-07 07:37:17
【问题描述】:

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

现在我在一台新电脑上工作,我想导入以前的 存储在外部磁盘中的 Postgres 数据库。

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

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

"db1" 20012
"db2" 23456

我想导出“db1”的架构。

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

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

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

【问题讨论】:

    标签: postgresql schema export


    【解决方案1】:

    你应该看看pg_dump

    pg_dump -s databasename
    

    只会将模式转储为 .sql 到标准输出。

    对于 Windows,您可能需要致电 pg_dump.exe。我无法访问 Windows 机器,但我很确定这是命令。看看帮助是否也适合您。

    【讨论】:

    • 你确定 pg_dump 会工作吗?原因是数据库的文件位于外部磁盘而不是本地...
    • 如果您可以启动旧的数据库服务器,可以。 pg_dump 可以远程连接到您可能知道的数据库服务器。我会尝试在您的新机器上安装 8.3 并复制您的旧数据库文件并启动 postgres 并查看是否有效。我以为您只是在更一般地询问有关仅转储架构的问题,我的错。
    • 我已经安装了8.3版本的postgresql,将data文件夹复制到新安装的,但是看不到我的旧数据库...
    • @Anew 谢谢它的工作。 pg_dump -s databasename > schema.sql 会将结果输出到schema.sql 文件中
    • 对我来说,我发现pg_dump $PSQL_CONNECT_STRING -s > myschema.sql 很有用,我在postgresql.org/docs/9.2/libpq-connect.html#LIBPQ-CONNSTRING 中设置了PSQL_CONNECT_STRING
    【解决方案2】:

    设置一个新的 postgresql 服务器并将其数据文件夹替换为外部磁盘中的文件。

    然后,您将能够启动该 postgresql 服务器并使用 pg_dump 检索数据(pg_dump -s 仅用于上述模式)

    【讨论】:

    • 我用旧的替换了data文件夹,然后我启动了服务,打开了pgAdmin III应用程序,点击了服务器图标[我只有1台服务器,前一个]。我要创建一个新服务器吗?因为它不起作用...我仍然看到我一开始创建的数据库...而不是备份数据库
    • 您确定将文件弹出到正确的数据文件夹中吗?我没有在 Windows 中使用过 postgresql,所以我不确定数据文件夹的位置。我也不确定 pgAdmin 是否缓存了任何内容,因此您可能还需要重新连接...
    • 好吧,我刚刚复制了 C:\Program Files\PostgreSQL\8.3 中的旧备份数据文件,并用新的替换了它。另外,当我再次打开 pgAdmin 时,它会询问旧计算机的密码,这是一个好兆头,但之后在数据库树中我看不到我的数据库
    • 嗯...我创建了一个名为“db1”的数据库[来自旧计算机的数据库],当我单击它时,pgAdmin 输出“发生错误:数据库 20012 的致命缓存查找失败“,这是什么意思?
    • 看起来pgAdmin缓存了很多信息。在 pgAdmin 中删除您的数据库连接并重新创建它。如果问题是关于缓存的,那应该可以解决它......
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      Linux 中你可以这样做

      pg_dump -U postgres -s postgres > exportFile.dmp
      

      也许它也可以在 Windows 中工作, 如果不尝试使用 pg_dump.exe

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

      【讨论】:

      • pg_dump: [archiver (db)] 连接到数据库“goldendemon”失败:端口号无效:“-s”
      【解决方案5】:

      我正在运行 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>.
      

      【讨论】:

        【解决方案6】:

        你应该使用类似pg_dump --schema=your_schema_name db1的东西,详情请看here

        【讨论】:

          【解决方案7】:
          pg_dump -d <databasename> -h <hostname> -p <port> -n <schemaname> -f <location of the dump file>
          

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

          【讨论】:

          • pg_dump -s -h -p -n -f 将标志从 -d 更改为 -s 以仅获取架构。正如其他人也提到的那样,这只是为了改善这一点。
          • 这将占用整个数据库:/
          【解决方案8】:

          对于 Linux:(数据除外)

          • pg_dump -s -t tablename databasename &gt; dump.sql(针对数据库中的特定表)

          • pg_dump -s databasename &gt; dump.sql(针对整个数据库)

          【讨论】:

          • (也适用于 mac;)
          【解决方案9】:

          pg_dump -s databasename -t tablename -U user -h host -p port &gt; tablename.sql

          这会将模式转储限制为“databasename”的表“tablename”

          【讨论】:

            猜你喜欢
            • 2011-06-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-10-16
            • 2011-09-24
            • 2010-10-22
            相关资源
            最近更新 更多