如何将数据从 Windows 上的 postgres 移动到 Linux 上的 postgres (Ubuntu)

How to move data from postgres on Windows to postgres on Linux (Ubuntu)

我在 Windows OS 的机器上有 Postgres DB,我还有 Linux (Ubuntu) 的虚拟机。我需要将数据从 Windows 移动到 Linux。 提前致谢!

您可以使用以下查询将数据库从一台服务器还原到另一台服务器

pg_dump -C -h SourceServer -U SourceUser  SourceDB | psql -h TargetHost -U TargetUser  TargetDB

密码可以从pgpass.conf

使用

我必须将数据库从我的本地机器 (Windows 11) 传输到 Linux (Ubuntu Bionic) 服务器,所以我做了以下操作:

  1. 使用以下命令将我的数据库转储到扩展名为 .sql 的备份文件中
  • 打开终端

  • 转到安装 postgresql 的文件夹,在我的例子中是

    C:\Program Files\PostgreSQL\bin
    
  • 在终端写

    cd C:\Program Files\PostgreSQL\bin
    
  • 键入以下命令,根据需要更改 并按回车键。

    pg_dump.exe -U postgres -d <db_name> -f D:\<folder_name>\<db_name>.sql
    
  • 输入您的密码,它将在提到的文件夹中创建备份。


  1. 连接到 Linux 服务器并复制 .sql 文件

    scp  D:\<folder_name>\<db_name>.sql  username@ip_address:/<destination_folder>
    

  1. 在服务器上安装 postgresql

    sudo apt-get install postgresql postgresql-contrib
    
  • 查看状态

    service postgresql status
    
  • 创建数据库

    sudo su postgres
    psql -U postgres -c “create database <db_name>”
    
  • 将转储文件中的数据导入到这个新数据库中。

    psql db_name < /path/db_name.sql