在本地,我使用pgadmin3。然而,在远程服务器上,我没有这样的特权。
我已经创建了数据库备份并复制了它,但是是否有一种方法可以从命令行恢复备份?我只看到与GUI或pg_dumps相关的东西。
在本地,我使用pgadmin3。然而,在远程服务器上,我没有这样的特权。
我已经创建了数据库备份并复制了它,但是是否有一种方法可以从命令行恢复备份?我只看到与GUI或pg_dumps相关的东西。
当前回答
当需要备份数据或从备份中恢复数据时,使用以下命令:
要创建数据备份,转到您的postgres\ bin\目录,如C:\programfiles\postgres\10\bin\,然后键入以下命令: pg_dump -FC -U ngb -d ngb -p 5432 >C:\BACK_UP\ngb. 090718_after_readupload .backup 要从备份中恢复数据,转到您的postgres\ bin\目录,如C:\programfiles\postgres\10\bin\,然后键入以下命令: C:\programFiles\postgres\10\bin> pg_restore -Fc -U ngb -d ngb -p 5432 <C:\ programFiles\postgres\10\bin 请确保备份文件存在。
其他回答
备份与恢复
这是我用来备份,删除,创建和恢复我的数据库(在macOS和Linux上)的组合:
sudo -u postgres pg_dump -Fc mydb > ./mydb.sql
sudo -u postgres dropdb mydb
sudo -u postgres createdb -O db_user mydb
sudo -u postgres pg_restore -d mydb < ./mydb.sql
Misc
-Fc将压缩数据库(格式自定义) 列出PostgreSQL用户:sudo -u postgres psql -c "\du+" 您可能需要在./mydb中添加主机名和日期。Sql,然后修改它: 。/主机名的_mydb_“日期+ Y“% % m % d_ % H % m”的. sql
如果你有一个备份SQL文件,那么你可以很容易地恢复它。 只要按照下面给出的说明做就可以了
1. At first, create a database using pgAdmin or whatever you want (for example my_db is our created db name)
2. Now Open command line window
3. Go to Postgres bin folder. For example: cd "C:\ProgramFiles\PostgreSQL\pg10\bin"
4. Enter the following command to restore your database: psql.exe -U postgres -d my_db -f D:\Backup\backup_file_name.sql
如果需要,请为postgres用户输入密码,然后让postgres执行它的工作。查看恢复进程。
恢复转储文件
psql -d [Dbname] -U [UserName] -p 5432 < [FileLocation]
恢复一个.SQL文件
pg_restore -U [Username] -d [Dbname] -1 [FileLocation]
如果出现用户身份验证错误,请转到程序文件PSQL/data文件夹中的pg_hba.conf文件,并将“METHOD”更改为“Trust”。 重新启动windows服务中的psql serive (Win + R—> services.msc)。
请看下面的例子,它的工作
C:/Program Files/PostgreSQL/9.4/bin\pg_restore.exe——host localhost——port 5432——username "postgres"——dbname "newDatabase"——no-password——verbose
“C: \用户下载\优\ \新下载\ DB.backup”
根据创建转储文件的方式,有两个工具可供参考。
您的第一个引用源应该是手册页pg_dump,因为它创建了转储本身。它说:
Dumps can be output in script or archive file formats. Script dumps are plain-text files containing the SQL commands required to reconstruct the database to the state it was in at the time it was saved. To restore from such a script, feed it to psql(1). Script files can be used to reconstruct the database even on other machines and other architectures; with some modifications even on other SQL database products. The alternative archive file formats must be used with pg_restore(1) to rebuild the database. They allow pg_restore to be selective about what is restored, or even to reorder the items prior to being restored. The archive file formats are designed to be portable across architectures.
所以这取决于它被倾倒的方式。如果使用Linux/Unix,您可能可以使用excellent file(1)命令来找出它-如果它提到ASCII文本和/或SQL,它应该用psql恢复,否则您可能应该使用pg_restore。
恢复非常简单:
psql -U username -d dbname < filename.sql
-- For Postgres versions 9.0 or earlier
psql -U username -d dbname -1 -f filename.sql
or
pg_restore -U username -d dbname -1 filename.dump
查看它们各自的手册页-有相当多的选项会影响恢复的工作方式。在恢复之前,您可能必须清除“活动”数据库或从template0重新创建它们(正如评论中指出的那样),这取决于转储是如何生成的。