Postgresql 数据库导出到 .sql 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37984733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Postgresql Database export to .sql file
提问by moxmlb
I want to export my database as a .sql file. Can someone help me? The solutions I have found don't work. A detailed description please. On Windows 7. Thanks
我想将我的数据库导出为 .sql 文件。有人能帮我吗?我找到的解决方案不起作用。请详细说明。在 Windows 7 上。谢谢
回答by Vao Tsun
pg_dumpdefaults to plain SQL export. both data and structure.
pg_dump默认为纯 SQL 导出。数据和结构。
open command promptand
run pg_dump -U username -h localhost databasename >> sqlfile.sql
打开命令提示符并运行pg_dump -U username -h localhost databasename >> sqlfile.sql
Above command is preferable as most of the times there will be an error which will be something like - ...FATAL: Peer authentication failed for user ...
上面的命令是可取的,因为大多数情况下都会出现错误,类似于 - ...FATAL: Peer authentication failed for user ...
回答by Chirag goyal
In windows, first, make sure the path is added in environment variables PATH
在windows中,首先确保在环境变量PATH中添加了路径
C:\Program Files\PostgreSQL\12\bin
C:\Program Files\PostgreSQL\12\bin
After a successful path adding restart cmd and type command
路径添加成功后重新启动cmd并输入命令
pg_dump -U username -p portnumber -d dbname -W -f location
pg_dump -U username -p portnumber -d dbname -W -f location
this command will export both schema and data
此命令将导出架构和数据
for only schema use -s in place of -W for only data use -a.
仅用于模式使用 -s 代替 -W 仅用于数据使用 -a。
replace each variable like username, portnumber, dbname and location according to your situation everything is case sensitive, make sure you insert everything correctly,and to import
根据您的情况替换每个变量,如用户名、端口号、dbname 和位置, 一切都区分大小写,确保正确插入所有内容,然后导入
psql -h hostname -p port_number -U username -f your_file.sql databasename
psql -h hostname -p port_number -U username -f your_file.sql databasename
make sure your db is created or creation query is present in .sql file
确保您的数据库已创建或创建查询存在于 .sql 文件中