How to export data from postgresql and reload

本文介绍如何使用PostgreSQL内置的pg_dump命令在Ubuntu和Windows服务器间高效同步数据,通过导出和导入SQL文件实现数据一致性。适用于小型数据集的同步,不建议用于大型数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Well, Recently I need to work on ubuntu and windows temporately. There are two postgresql servers that are running on ubuntu and windows respectively. But I need the data on both servers is consistent, so I need export the data then reload it after switching workstation. 

 

After doing some research, I found one build-in command in postgresql server does the tricks. pg_dump and pg_dumpall. Here I only use pg_dump to accomplish my task.

 

Export:

 

pg_dump -c scportal -h localhost -U ethanz > /home/ethanz/database_dump1.sql

 

 

-c
--clean
Output commands to clean (drop) database objects prior to outputting the commands for creating them. (Restore might generate some harmless errors.)

This option is only meaningful for the plain-text format. For the archive formats, you can specify the option when you call pg_restore.
 

That is exactly what I need, connect to my local server, export the sql.

 

Then I can put the exported sql to svn or wherever. After switching the workstation, I'm able to synchrnize the data from the output sql file

 

Load:

ethanz@ethanz-U:~$ psql -d scportal -h localhost -U ethanz -f /home/ethanz/database_dump1.sql

 Then postgresql will automatically drop the database, index, table, sequence and recreate them then load the data. I think it's a good solution to resynchronize small buntch of data. It's not suitable to huge database.

 

 

This article is only for reference.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值