从原始物理文件恢复 postgreSQL 数据库

发布于 2024-09-11 13:26:41 字数 446 浏览 9 评论 0原文

我有以下问题,我需要知道是否有办法解决它。

我有一个客户,他足够便宜,拒绝在运行他公司的主系统上为其 postgreSQL 数据库购买备份计划,而且我认为有一天会发生这种情况,一些操作系统文件在停电期间崩溃了,需要重新安装操作系统。

该客户端没有任何数据库备份,但我设法保存了 PostgreSQL 主目录。我读到数据库以某种方式存储在 postgres 主文件夹的数据目录中。

我的问题是:有什么方法可以仅从数据文件夹中恢复数据库吗?我正在使用 PostgreSQL 8.2 的 Windows 环境(XP Service Pack 2)中工作,我需要在新服务器中重新安装 PostgreSQL。我需要在新环境中重新创建数据库,并以某种方式将旧文件附加到新数据库实例。我知道这在 SQL Server 中是可能的,因为引擎存储数据库的方式,但我对 postgres 没有任何线索。

有什么想法吗?他们将不胜感激。

I have the following problem and I need to know if there´s a way to fix it.

I have a client who was cheap enough to decline buying a backup plan for his postgreSQL databases on the main system that runs his company and as I thought it would happen some day, some OS files crashed during a blackout and the OS needs to be reinstalled.

This client didn't have any backups of the databases but I managed to save the PostgreSQL main directory. I read that the databases are stored somehow inside the data directory of the postgres main folder.

My question is: Is there any way to recover the databases from the data folder only? I am working in a windows environment (XP service pack 2) with PostgreSQL 8.2 and I need to reinstall PostgreSQL in a new server. I would need to recreate the databases in the new environment and somehow attach the old files to the new database instances. I know that's possible in SQL Server because of the way that engine stores the databases but I have no clue in postgres.

Any ideas? They would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

江心雾 2024-09-18 13:26:41

如果您拥有整个数据文件夹,那么您就拥有了所需的一切(只要架构相同)。只要尝试在另一台计算机上恢复它,然后再擦除这台计算机,以防您没有复制某些内容。

只需将数据目录保存到磁盘即可。启动 Postgres 时,设置告诉它数据目录所在位置的参数(请参阅:wiki.postgresql.org)。或者删除全新安装的原始数据目录并将副本放在其位置。

If you have the whole data folder, you have everything you need (as long as architecture is the same). Just try restoring it on another machine before wiping this one out, in case you didn't copy something.

Just save the data directory to disk. When launching Postgres, set the parameter telling it where the data directory is (see: wiki.postgresql.org). Or remove original data directory of the fresh installation and place the copy in its place.

渔村楼浪 2024-09-18 13:26:41

这是可能的,您只需将“data”文件夹(在 Postgres 安装文件夹内)从旧计算机复制到新计算机,但有一些事情需要记住。

首先,在复制文件之前,必须停止 Postgres 服务器服务。所以,控制面板->管理工具->服务,找到Postgres服务并将其停止。完成文件复制和设置权限后,重新启动。

其次,您需要设置数据文件的权限。因为 postgres 服务器实际上在另一个用户帐户上运行,所以如果您只是将文件复制到数据文件夹中,它将无法访问这些文件,因为它没有这样做的权限。因此,您需要将文件的所有权更改为“postgres”用户。我必须使用 subinacl首先安装它,然后从命令提示符中使用它,如下所示(首先导航到安装它的文件夹):(

subinacl /subdirectories "C:\Program Files\PostgreSQL\8.2\data\*" /setowner=postgres

也可以从资源管理器中更改所有权:首先,您必须禁用“使用简单文件共享”文件夹选项,然后“安全”选项卡将出现在文件夹属性对话框中,其中有用于设置权限和更改所有权的选项,但我无法这样做。)

现在,如果服务器服务可以'再次手动启动后无法启动,通常可以在事件查看器(管理工具->事件查看器)中看到原因。 Postgres 会抛出一个错误事件,检查它会给你一个关于问题所在的线索(有时它会抱怨 postmaster.pid 文件,只需删除它,等等)。

This is possible, you just need to copy the "data" folder (inside the Postgres installation folder) from the old computer to the new one, but there are a few things to keep in mind.

First, before you copy the files, you must stop the Postgres server service. So, Control Panel->Administrative tools->Services, find Postgres service and stop it. When you're done copying the files and setting permissions, start it again.

Second, you need to set the permissions for the data files. Because postgres server actually runs on another user account, it will not be able to access the files if you just copy them into the data folder, because it will not have permissions to do so. So you need to change the ownership of the files to the "postgres" user. I had to use subinacl for this, install it first, and then use it from command prompt like this (first navigate to folder where you installed it):

subinacl /subdirectories "C:\Program Files\PostgreSQL\8.2\data\*" /setowner=postgres

(Changing ownership should also be possible to do from the explorer: first you must disable "Use simple file sharing" in Folder options, then a "Security" tab will appear in the folder Properties dialog, and there are options there to set permissions and change ownership, but I wasn't able to do it that way.)

Now, if the server service can't start after you start it manually again, you can usually see the reason in the Event viewer (Administrative tools->Event viewer). Postgres will throw an error event, and inspecting it will give you a clue about what the problem is (sometimes it will complain about a postmaster.pid file, just remove it, etc.).

怪我太投入 2024-09-18 13:26:41

这个问题很老了,但我想分享一个我发现的有效方法。

如果您没有使用“pg_dump”进行备份并且您的旧数据是文件夹,请尝试以下步骤。
在 Postgres 数据库中,将记录添加到“pg_database”表中。使用管理器程序或“插入”。
进行必要的检查并更改以下插入查询并运行它。

查询成功后将返回一个 OID。使用该号码的名称创建一个文件夹。将旧数据复制到此文件夹后,即可使用。



    /*
    ------------------------------------------
    *** Recover From Folder ***
    ------------------------------------------
    Check this table on your own system.
    Change the differences below.
    */
    INSERT INTO
      pg_catalog.pg_database(
      datname, datdba, encoding, datcollate, datctype, datistemplate, datallowconn,
      datconnlimit, datlastsysoid, datfrozenxid, datminmxid, dattablespace, datacl)
    VALUES(
                             -- Write Your collation  
      'NewDBname', 10, 6, 'Turkish_Turkey.1254', 'Turkish_Turkey.1254',
      False, True, -1, 12400, '536', '1', 1663, Null);

    /*
    Create a folder in the Data directory under the name below New OID.
    All old backup files in the directory "data\base\Old OID" are the directory with the new OID number
    Copy. The database is now ready for use.
    */
    select oid from pg_database a where a.datname = 'NewDBname';

The question is very old, but I want to share an effective method that I found.

If you have not got a backup with "pg_dump" and your old data is folder, try the following steps.
In the Postgres database, add records to the "pg_database" table. With a manager program or "insert into".
Make the necessary check and change the following insert query and run it.

The query will return an OID after it has worked. Create a folder with the name of this number. Once you have copied your old data into this folder, the use is now ready.



    /*
    ------------------------------------------
    *** Recover From Folder ***
    ------------------------------------------
    Check this table on your own system.
    Change the differences below.
    */
    INSERT INTO
      pg_catalog.pg_database(
      datname, datdba, encoding, datcollate, datctype, datistemplate, datallowconn,
      datconnlimit, datlastsysoid, datfrozenxid, datminmxid, dattablespace, datacl)
    VALUES(
                             -- Write Your collation  
      'NewDBname', 10, 6, 'Turkish_Turkey.1254', 'Turkish_Turkey.1254',
      False, True, -1, 12400, '536', '1', 1663, Null);

    /*
    Create a folder in the Data directory under the name below New OID.
    All old backup files in the directory "data\base\Old OID" are the directory with the new OID number
    Copy. The database is now ready for use.
    */
    select oid from pg_database a where a.datname = 'NewDBname';

只是一片海 2024-09-18 13:26:41

As shown by move database to another hard drive. All we need to do is to modify the registry table and file permissions. By modifying registry table(shown in image 1), postgresql server know the new location of data.

modify registry

modify registry

一瞬间的火花 2024-09-18 13:26:41

如果您在安装到旧数据文件夹期间遇到权限问题或 icacls 等问题,请尝试我的姐妹网站上的解决方案。

https://superuser.com/a/1611934/1254226

If you have issues with permissions or with stuff like icacls during installation to old data folder then try my solution from sister website.

https://superuser.com/a/1611934/1254226

多像笑话 2024-09-18 13:26:41

我这样做,但最棘手的部分是更改所有者权限:

  1. 从管理工具转到服务,
  2. 双击它,
  3. 找到 postgres 服务,然后在登录选项卡上
  4. 更改为本地系统,然后重新启动

I do so but the most tricky part was to change the owner permission:

  1. go to services from administative tools
  2. find postgres service and double click on it
  3. at log on tab change to local system
  4. then restart
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文