如何从 Postgres 转储中 pg_restore 一个 table 及其模式?
How to pg_restore one table and its schema from a Postgres dump?
我在恢复 table 的架构时遇到了一些困难。我转储了我的 Heroku Postgres 数据库,并使用 pg_restore 将其中的一个 table 恢复到我的本地数据库(它有超过 20 个 table)。它已成功恢复,但是当我尝试将新数据插入 table 时遇到问题。
当我使用 psql 打开我的数据库时,我发现恢复的 table 可用于所有数据,但它的模式有零行。无论如何,我可以从转储中导入 table 及其模式吗?非常感谢。
这就是我将 table 恢复到本地数据库的方式:
pg_restore -U postgres --dbname my_db --table=message latest.dump
编辑:
我按照官方文档尝试了类似的操作,但它只是被阻止,没有任何反应。我的数据库很小,不超过几兆字节,我要恢复的 table 的架构不超过 100 行。
pg_restore -U postgres --dbname mydb --table=message --schema=message_id_seq latest.dump
来自 Heroku DevCenter here
Heroku Postgres is integrated directly into the Heroku CLI and offers
many helpful commands that simplify common database tasks
如果您的环境配置正确,您可以检查here。
通过这种方式,您可以使用 Heroku CLI pg:pull 命令将远程数据从 Heroku Postgres 数据库拉取到您机器上的本地数据库。
例如:
$ heroku pg:pull HEROKU_POSTGRESQL_MAGENTA mylocaldb --app sushi
作为更笼统的答案(我需要从 庞大的 备份中恢复单个 table,您可能想看看这个 post: https://thequantitative.medium.com/restoring-individual-tables-from-postgresql-pg-dump-using-pg-restore-options-ef3ce2b41ab6
# run the schema-only restore as root
pg_restore -U postgres --schema-only -d new_db /directory/path/db-dump-name.dump
# Restore per table data using something like
pg_restore -U postgres --data-only -d target-db-name -t table_name /directory/path/dump-name.dump
我在恢复 table 的架构时遇到了一些困难。我转储了我的 Heroku Postgres 数据库,并使用 pg_restore 将其中的一个 table 恢复到我的本地数据库(它有超过 20 个 table)。它已成功恢复,但是当我尝试将新数据插入 table 时遇到问题。
当我使用 psql 打开我的数据库时,我发现恢复的 table 可用于所有数据,但它的模式有零行。无论如何,我可以从转储中导入 table 及其模式吗?非常感谢。
这就是我将 table 恢复到本地数据库的方式:
pg_restore -U postgres --dbname my_db --table=message latest.dump
编辑:
我按照官方文档尝试了类似的操作,但它只是被阻止,没有任何反应。我的数据库很小,不超过几兆字节,我要恢复的 table 的架构不超过 100 行。
pg_restore -U postgres --dbname mydb --table=message --schema=message_id_seq latest.dump
来自 Heroku DevCenter here
Heroku Postgres is integrated directly into the Heroku CLI and offers many helpful commands that simplify common database tasks
如果您的环境配置正确,您可以检查here。
通过这种方式,您可以使用 Heroku CLI pg:pull 命令将远程数据从 Heroku Postgres 数据库拉取到您机器上的本地数据库。
例如:
$ heroku pg:pull HEROKU_POSTGRESQL_MAGENTA mylocaldb --app sushi
作为更笼统的答案(我需要从 庞大的 备份中恢复单个 table,您可能想看看这个 post: https://thequantitative.medium.com/restoring-individual-tables-from-postgresql-pg-dump-using-pg-restore-options-ef3ce2b41ab6
# run the schema-only restore as root
pg_restore -U postgres --schema-only -d new_db /directory/path/db-dump-name.dump
# Restore per table data using something like
pg_restore -U postgres --data-only -d target-db-name -t table_name /directory/path/dump-name.dump