如何在没有备份的情况下恢复postgresql数据库
忘记备份了。现在我有带有数据库的硬盘和带有空 postgres 的新系统。我可以以某种方式恢复数据库吗?通过简单的文件副本等?
Forgot to make a backup. Now I have harddrive with databases and new system with empty postgres. Can I somehow restore databases? by simple copy of files etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有旧 postgresql 系统的完整data目录(并且如果它是相同版本,或者仅修订号不同),您可以尝试将其放在您的位置新安装的 postgresql 中的 data 目录。 (当然,在执行此操作之前,请先停止 postgres 服务器)。
这基本上与 升级 postgresql 时使用的过程相同,当有无需进行备份恢复。
编辑:正如评论中所指出的,我假设不仅是相同(或几乎相同)的版本,而且是相同的架构(32 - 64 位,Linux - Windows 等)
If you have the full data directory of your old postgresql system (and if it was the same version, or differing only in a revision number) you can just try to put it in place of your data directory in your new postgresql installation. (Of course, stop postgres server before doing this).
It's basically the same procedure used when upgrading postgresql, when there is no need to do backup-restore.
Edit: As pointed out in the comments, I assume not only same (or almost same) version, but same architecture (32 - 64 bits , Linux - Windows, etc)
除了 leonbloy 的答案之外,您还可以尝试 pg_migrator,特别是如果您需要从8.3 到 8.4(最终是 9.0)。
In addition to the leonbloy's answer, you could try pg_migrator, especially if you need to upgrade from 8.3 to 8.4 (and 9.0 eventually).
在你的情况下,你有这些文件,但如果你没有,也许,只是也许,你可以对数据库的日志做一些事情,你可以尝试在 /var/log 中正常查看数据库中的语句日志/postgresql/postgresql.log,如果它在那里或接近它,并且如果之前设置了 log_statements = 'mod' 或 'all',则可以恢复一些数据。
逐个表,通过插入到数据库的所有或最近历史记录中的该表中进行搜索。您可以使用一些 Unix 工具剪切文本以仅获取语句并放置一个“;”在每个语句的末尾,以及另一个重要的查询,如删除等。
但是您必须逐表执行,并且数据必须在那里,并且数据库不会在没有备份的情况下运行太多时间。
在某些情况下,您只需要最后一次操作或类似的操作即可挽救局面。
然而,这只是阿波罗 13 号灾难时刻,永远无法取代良好的备份。
In your case you have the files, but if you haven't, Maybe, only maybe, you can do something with the logs of the database, you can try to see the log of the statements in the database normally in /var/log/postgresql/postgresql.log, if it is there or close to it, and if log_statements = 'mod' or 'all' is set up before, you can recovery some of your data.
Table by table, by searching by insert into in this tables in all or recent history of database. You can cut text with some Unix tools to get only the statements and put a ";" at the end of each statement, and another important queries like delete, etc.
But you must to do it table by table, and data must be there, and database don't runned up too much time without backups.
In certain cases you just need the last operation or something like this to save the day.
This, however, its just for Apolo 13 disasters moment and never can replace a good backup.