使用命令行恢复 postgres 备份文件?
在本地,我使用 pgadmin3。然而,在远程服务器上,我就没有这样的奢侈了。
我已经创建了数据库的备份并将其复制过来,但是有没有办法从命令行恢复备份?我只看到与 GUI 或 pg_dumps 相关的内容。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在本地,我使用 pgadmin3。然而,在远程服务器上,我就没有这样的奢侈了。
我已经创建了数据库的备份并将其复制过来,但是有没有办法从命令行恢复备份?我只看到与 GUI 或 pg_dumps 相关的内容。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(30)
有两种工具可供查看,具体取决于您创建转储文件的方式。
您的第一个参考来源应该是手册页 pg_dump 因为这就是自己创建转储。它说:
所以取决于它被倾倒的方式。如果使用 Linux/Unix,您可能可以使用出色的
file(1)
命令来弄清楚 - 如果它提到 ASCII 文本和/或 SQL,则应该使用 psql 否则你应该使用 pg_restore。恢复非常简单:
或者
查看它们各自的联机帮助页 - 有很多选项会影响恢复的工作方式。在恢复之前,您可能必须清理“实时”数据库或从 template0 重新创建它们(如评论中指出的),具体取决于转储的生成方式。
There are two tools to look at, depending on how you created the dump file.
Your first source of reference should be the man page pg_dump as that is what creates the dump itself. It says:
So depends on the way it was dumped out. If using Linux/Unix, you can probably figure it out using the excellent
file(1)
command - if it mentions ASCII text and/or SQL, it should be restored with psql otherwise you should probably use pg_restore.Restoring is pretty easy:
or
Check out their respective manpages - there's quite a few options that affect how the restore works. You may have to clean out your "live" databases or recreate them from template0 (as pointed out in a comment) before restoring, depending on how the dumps were generated.
创建备份
-F c
是自定义格式(已压缩,并且能够与-j N
并行执行)-b
是包括 blob,-v
是详细,-f
是备份文件名。从备份恢复
设置
-h localhost
- 选项很重要create backup
-F c
is custom format (compressed, and able to do in parallel with-j N
)-b
is including blobs,-v
is verbose,-f
is the backup file name.restore from backup
important to set
-h localhost
- option您可能需要以
postgres
身份登录才能拥有数据库的完全权限。pg_dump/pg_restore
开关
-F
指定备份文件的格式:c
将使用自定义的PostgreSQL格式,该格式经过压缩并产生最小的备份文件大小t
表示 TAR 存档(大于自定义格式)-h
/--host 指定运行服务器的计算机的主机名
-W
/--password
强制pg_dump
在之前提示输入密码连接到数据库恢复备份:
参数
-C
应在导入数据之前创建数据库。如果它不起作用,您可以随时创建数据库,例如。使用命令(作为用户postgres
或有权创建数据库的其他帐户)createdb db_name -O Owner
pg_dump/psql
如果您没有未指定参数
-F
使用默认纯文本 SQL 格式(或使用-F p
)。那么你就不能使用pg_restore
。您可以使用psql
导入数据。备份:
恢复:
You might need to be logged in as
postgres
in order to have full privileges on databases.pg_dump/pg_restore
switch
-F
specify format of backup file:c
will use custom PostgreSQL format which is compressed and results in smallest backup file sized
for directory where each file is one tablet
for TAR archive (bigger than custom format)-h
/--host
Specifies the host name of the machine on which the server is running-W
/--password
Forcepg_dump
to prompt for a password before connecting to a databaserestore backup:
Parameter
-C
should create database before importing data. If it doesn't work you can always create database eg. with command (as userpostgres
or other account that has rights to create databases)createdb db_name -O owner
pg_dump/psql
In case that you didn't specify the argument
-F
default plain text SQL format was used (or with-F p
). Then you can't usepg_restore
. You can import data withpsql
.backup:
restore:
POSTGRESQL 9.1.12
DUMP:
输入用户密码并按 Enter。
恢复:
输入用户密码并按 Enter 键。
POSTGRESQL 9.1.12
DUMP:
put the user password and press enter.
RESTORE:
put the user password and press enter.
下面是我用来恢复数据库的 pg_dump 版本:
或者使用 psql:
其中
-h
主机,-p< /code> 端口,
-u
登录用户名,-d
数据库名称Below is my version of
pg_dump
which I use to restore the database:or use
psql
:where
-h
host,-p
port,-u
login username,-d
name of database使用 GZIP 备份和恢复
备份
恢复
https://www.postgresql.org /docs/14/backup-dump.html
Backup and restore with GZIP
backup
restore
https://www.postgresql.org/docs/14/backup-dump.html
这对我有用:
This worked for me:
备份与备份恢复
这是我用来备份、删除、创建和恢复我的数据库(在 macOS 和 Linux 上)的组合:
Misc
-Fc
将压缩数据库(Format c ustom)sudo -u postgres psql -c "\du+"
./mydb.sql
,然后通过以下方式更改:Backup & Restore
This is the combo I'm using to backup, drop, create and restore my database (on macOS and Linux):
Misc
-Fc
will compress the database (Format custom)sudo -u postgres psql -c "\du+"
./mydb.sql
, then change it by:试试这个:
从 .sql 文件恢复 DB psql
try this:
Restore DB psql from .sql file
如果您正在使用 docker,这个答案可能会有所帮助。
.tar
备份文件复制到 docker 容器(在另一个窗口中)If you are using docker, this answer may be helpful.
.tar
backup file to docker container (In another window)1.打开终端。
2.使用以下命令备份您的数据库:
您的 postgres bin ->
/opt/PostgreSQL/9.1/bin/
你的源数据库服务器 ->
192.168.1.111
您的备份文件位置和名称 ->
/home/dinesh/db/mydb.backup
你的源数据库名称 ->
我的数据库
3。将 mydb.backup 文件恢复到目标位置。
你的目标服务器 ->
localhost
你的目标数据库名称 ->
mydatabase
创建数据库以恢复备份。
恢复备份。
1. Open the Terminal.
2. Backup your database with following command
your postgres bin ->
/opt/PostgreSQL/9.1/bin/
your source database server ->
192.168.1.111
your backup file location and name ->
/home/dinesh/db/mydb.backup
your source db name ->
mydatabase
3. Restore mydb.backup file into destination.
your destination server ->
localhost
your destination database name ->
mydatabase
Create database for restore the backup.
Restore the backup.
如果您使用 pg_dump 创建备份,您可以通过以下方式轻松恢复它:
cd "C:\ProgramFiles\PostgreSQL\9.5\bin"
例如:psql.exe -U postgres -d YourDatabase -f D:\Backup\.sql
If you create a backup using pg_dump you can easily restore it in the following way:
cd "C:\ProgramFiles\PostgreSQL\9.5\bin"
For example: psql.exe -U postgres -d YourDatabase -f D:\Backup\.sql
恢复转储文件
恢复 .SQL 文件
如果出现用户身份验证错误,请转至程序文件中 PSQL/data 文件夹中的 pg_hba.conf 文件,并将“METHOD”更改为“Trust”。
在 Windows 服务中重新启动 psql 服务器(Win + R --> services.msc)。
To restore a dump file
To restore a .SQL file
If you get user authentication errors, go to the file pg_hba.conf which is in PSQL/data folder in your program files, and change the "METHOD" to "Trust".
Restart you psql serive in windows services(Win + R --> services.msc).
1)打开psql终端。
2) 解压/解压转储文件。
3)创建一个空数据库。
4)使用以下命令恢复.dump文件
1) Open psql terminal.
2) Unzip/ untar the dump file.
3) Create an empty database.
4) use the following command to restore the .dump file
尝试:
try:
我没有看到这里提到转储文件扩展名(*.dump)。
这个解决方案对我有用:
我得到了一个转储文件,需要恢复它。
首先,我尝试使用 pg_restore 执行此操作并得到:
我使用 psql 执行此操作并且运行良好:
I didnt see here mentions about dump file extension (*.dump).
This solution worked for me:
I got a dump file and needed to recover it.
First I tried to do this with
pg_restore
and got:I did it with
psql
and worked well:使用压缩转储保存和恢复完全相同的状态
其他答案分别给出了所有关键位,但希望这将提供您通常想要的“只需保存并恢复到准确状态”命令对。
将数据库转储到文件
mydb.psql
:从文件
mydb.psql
恢复数据库:一些标志:
-Fc
:格式“自定义”,它恰好也被压缩而不是纯文本,并生成一个小得多的转储文件。文档: https://www.postgresql.org/docs/current/app- pgdump.html文件 tmp.psql
说:还可以使用
-Z
选项进一步自定义压缩。--clean
:在恢复之前销毁目标数据库,从而返回到完全相同的原始状态。转储后创建的任何数据都将丢失。如果您不通过此选项,它会尝试将转储添加到预先存在的数据之上,这可能会违反唯一约束并损坏您的数据。-v
:详细,恢复时输出日志。可以帮助调试事情--no-privileges --no-owner
:防止发出设置权限的GRANT
命令。这些可能会导致问题,例如,如果您要在本地恢复生产数据库以进行重现,因为生产数据库和本地数据库可能具有不同的权限设置。另请参阅:pg_restore 错误:角色 XXX 不存在< /p>< code>PGPASSWORD、
-U
和-h
当然可以根据您的登录方法进行修改,例如没有PGPASSWORD
您'重新提示输入密码,如果您在本地设置对等身份验证,则不需要这些密码。在 Ubuntu 22.04、PostgreSQL 14.5 上测试。
Save and restore the exact same state with compressed dump
Other answers gave all the key bits separately, but hopefully this will provide be the "just works save and restore to exact state" command pair that you usually want.
Dump the DB to a file
mydb.psql
:Restore the DB from a file
mydb.psql
:Some of the flags:
-Fc
: Format "custom", which happens to also be compressed as opposed to plaintext, and produces a much smaller dump file. Docs: https://www.postgresql.org/docs/current/app-pgdump.htmlfile tmp.psql
says:Compression can also be further customized with the
-Z
option.--clean
: destroy the target DB before restoring it, thus returning to the exact same pristine state. Any data created after the dump will be lost. If you don't pass this, it attempts to just add the dump on top of pre-existing data, which could fail unique constraints and corrupt your data.-v
: verbose, output logs while restore. Can be helpful do debug things--no-privileges --no-owner
: prevent issuingGRANT
commands that setup privileges. These can cause trouble e.g. if you are restoring a production DB locally to reproduce, as the production and local may have different privilege setups. See also: pg_restore error: role XXX does not existPGPASSWORD
,-U
and-h
can of course be modified depending on your login method, e.g. withoutPGPASSWORD
you're prompted for a password, and none of those are needed if you set up peer auth locally.Tested on Ubuntu 22.04, PostgreSQL 14.5.
恢复 postgres 备份文件取决于您最初如何进行备份。
如果您将 pg_dump 与 -F c 或 -F d 一起使用,则需要使用 pg_restore 否则您可以使用
9种备份和恢复postgres数据库的方法
Restoring a postgres backup file depends on how did you take the backup in the first place.
If you used pg_dump with -F c or -F d you need to use pg_restore otherwise you can just use
9 ways to backup and restore postgres databases
无需密码提示的最短方法
如果您使用的是 Windows 操作系统,则
The shortest way with no password prompt
If you are using Windows OS
正如下面的链接所述,您可以使用 psql 命令来恢复转储文件:
https://www.postgresql.org/docs/8.1/static/backup.html#BACKUP-DUMP-RESTORE
如果您需要设置用户名,只需在命令后面添加用户名,例如:
As below link said, you can use psql command for restoring the dump file:
https://www.postgresql.org/docs/8.1/static/backup.html#BACKUP-DUMP-RESTORE
if you need to set username just add the username after the command like:
我在运行 pg_dump 时遇到身份验证问题,因此我将转储文件移动
到临时目录中,然后运行
如果您有一个大型数据库转储,您可能只想创建另一个目录,当前用户和 postgres 用户可以在其中访问并放置数据库转储文件到其中。
I was having authentication problems running pg_dump, so I moved my dump file
into the temp directory and then ran
If you have a large database dump, you may just want to create another directory where your current user and the postgres user can access and putting the database dump file into that.
尝试看看以下命令是否可以帮助您:
Try to see if the following commands can help you:
如果您有备份 SQL 文件,那么您可以轻松恢复它。
只需按照下面给出的说明进行操作,
如果需要,请输入您的 postgres 用户的密码,然后让 Postgres 完成其工作。然后您可以检查恢复过程。
If you have a backup SQL file then you can easily Restore it.
Just follow the instructions, given in the below
Type password for your postgres user if needed and let Postgres to do its work. Then you can check the restore process.
抱歉,死尸帖子,但这些解决方案对我不起作用。我使用的是 postgres 10。在 Linux 上:
service postgresql-10 restart
将目录更改为我的 backup.sql 所在位置并执行:
psql postgres -d 数据库名称 -1 -f backup.sql
-database_name 是我的数据库的名称
-backup.sql 是我的 .sql 备份文件的名称。
Sorry for the necropost, but these solutions did not work for me. I'm on postgres 10. On Linux:
service postgresql-10 restart
Change directory to where my backup.sql was located and execute:
psql postgres -d database_name -1 -f backup.sql
-database_name is the name of my database
-backup.sql is the name of my .sql backup file.
例如,您可以将用户(角色)
john
的apple
数据库的架构和数据导出到backup.sql
,使用 < a href="https://www.postgresql.org/docs/current/app-psql.html" rel="nofollow noreferrer">psql 必须用于导入非存档文件,如下所示。 *如果不存在,则创建backup.sql
并且我的答案解释了如何导出架构和数据以及我的回答解释了如何将存档backup.sql
导入orange
数据库和文档解释了如何导出和导入数据库:或者:
或者,您只导出特定表的数据用户(角色)
john
的apple
数据库到backup.sql
,仅使用INSERT
语句,其列名称为如下图:然后,运行上面的命令后需要输入密码:
现在,您可以将
backup.sql
导入到用户(角色)的orange
数据库中john
如下所示。 *您必须事先创建orange
数据库,否则会出现错误并且 我的回答解释了如何创建数据库:或:
或:
或:
或:
或:
或者,您可以尝试以下这些在 Windows 上不起作用的方法:
然后,您需要运行上述命令后输入密码:
此外,您可以将
backup.sql
导入到用户(角色)john
的orange
数据库中,而无需通过将密码(例如banana
)设置为 PGPASSWORD 如下图:For example, you export the schema and data or only the schema of
apple
database of the user(role)john
tobackup.sql
with psql which must be used to import non-archive files as shown below. *backup.sql
is created if it doesn't exist and my answer explains how to export schema and data and my answer explains how to import archivebackup.sql
intoorange
database and the doc explains how to export and import databases:Or:
Or, you export only the data of the specific tables of
apple
database of the user(role)john
tobackup.sql
with onlyINSERT
statement which has column names as shown below:Then, you need to input a password after running the command above:
Now, you can import
backup.sql
intoorange
database of the user(role)john
as shown below. *You have to createorange
database before hand otherwise there is error and my answer explains how to create a database:Or:
Or:
Or:
Or:
Or:
Or, you can try these below which don't work on Windows:
Then, you need to input a password after running the command above:
In addition, you can import
backup.sql
intoorange
database of the user(role)john
without a password prompt by setting a password(e.g.,banana
) to PGPASSWORD as shown below:备份==>
选项1:在cmd中与密码一起进行备份
1.PGPASSWORD="mypassword" pg_dump -U postgres -h localhost --inserts mydb>mydb.sql
选项2:在cmd中进行无密码备份
2. pg_dump -U postgres -h localhost --inserts mydb>mydb.sql
选项3:以gzip方式备份(如果数据库很大)
3. pg_dump -U postgres -h localhost mydb --inserts | gzip > mydb.gz
恢复:
<代码> 1. psql -h localhost -d mydb -U postgres -p 5432 < mydb.sql
Backup==>
Option1: To take backup along with password in cmd
1.PGPASSWORD="mypassword" pg_dump -U postgres -h localhost --inserts mydb>mydb.sql
Option2: To take backup without password in cmd
2. pg_dump -U postgres -h localhost --inserts mydb>mydb.sql
Option3: To take backup as gzip(if database is huge)
3. pg_dump -U postgres -h localhost mydb --inserts | gzip > mydb.gz
Restore:
1. psql -h localhost -d mydb -U postgres -p 5432 < mydb.sql
此解决方案仅适用于Windows。
首先,确保您已经将 postgres bin 文件夹添加到“Path”环境变量中(在我的例子中,该文件夹是 C:\Program Files\PostgreSQL\12\bin)。
然后,打开 Windows 命令解释器 (cmd),转到 .sql 文件所在的文件夹并执行以下命令:
pg_restore -U userName -d database-1 backupfile.sql
例如:
< strong>pg_restore -U sam -d SamDataBase -1 SamDataBaseBackup.sql
(它会要求您输入用户的密码,因此请确保正确输入密码,然后单击 Enter)
Pura vida!
This solution only works for Windows.
First, ensure you have already added the postgres bin folder to the "Path" environment variable (in my case this folder is C:\Program Files\PostgreSQL\12\bin).
Then, open the Windows command interpreter (cmd), go to the folder where you have the .sql file and execute this command:
pg_restore -U userName -d database-1 backupfile.sql
For example:
pg_restore -U sam -d SamDataBase -1 SamDataBaseBackup.sql
(It can ask you for the password of the user so ensure to type it correctly and then click enter)
Pura vida!
如果您创建了一个名为
mydb
的新数据库,要使用psql将.sql转储恢复到该数据库,psql将提示输入密码
连接选项是
If you have created a new database named
mydb
, To restore a .sql dump to that database with psql,the password will be prompted by psql
The connection options are
也许尝试这个命令它会起作用
Maybe try this command it will work