主页

postgres windows导出数据库

2025-05-28 08:46PM

1. 在 PowerShell 中,进入 PostgreSQL 的 bin 目录

$ cd "C:\Program Files\PostgreSQL\<version>\bin"

替换 <version> 为你安装的 PostgreSQL 版本号

2. 使用 pg_dump 导出数据库为 SQL 文件:

$  pg_dump -U <username> -W -F p -f "C:\path\to\output\database.sql" <database_name> 

但是报错了

PS C:\Program Files\PostgreSQL\14\bin> pg_dump -U postgres -W -F p -f "C:\workspace\dongtaipaifang_2025_05_28.sql" dongtaipaifang
pg_dump : The term 'pg_dump' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At line:1 char:1
+ pg_dump -U postgres -W -F p -f "C:\workspace\dongtaipaifang_2025_05_2 ...
+ ~~~~~~~
    + CategoryInfo          : ObjectNotFound: (pg_dump:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


Suggestion [3,General]: The command pg_dump was not found, but does exist in the current location. Windows PowerShell does not load commands from the current location by default. If you trust this command, instead type: ".\pg_dump". See "get-help about_Command_Precedence" for more details. 

原因是因为PowerShell 不会默认从当前目录执行命令

解决方案:

方法1- 使用相对路径:在当前目录下运行 pg_dump 时,使用 .\ 前缀:

$ .\pg_dump -U postgres -W -F p -f "C:\workspace\dongtaipaifang_2025_05_28.sql" dongtaipaifang 

eg:

PS C:\Program Files\PostgreSQL\14\bin> .\pg_dump -U postgres -W -F p -f "C:\workspace\dongtaipaifang_2025_05_28.sql" dongtaipaifang
Password:

方法2-使用cmd,在 PowerShell 中输入 cmd 来切换到命令提示符,然后再运行 pg_dump

$ cmd
$ cd "C:\Program Files\PostgreSQL\14\bin"
$ pg_dump -U postgres -W -F p -f "C:\workspace\dongtaipaifang_2025_05_28.sql" dongtaipaifang

这样就导出数据库数据了。 

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论