使用命令行恢复 postgres 备份文件?

发布于 2024-08-30 20:19:40 字数 117 浏览 5 评论 0 原文

在本地,我使用 pgadmin3。然而,在远程服务器上,我就没有这样的奢侈了。

我已经创建了数据库的备份并将其复制过来,但是有没有办法从命令行恢复备份?我只看到与 GUI 或 pg_dumps 相关的内容。

Locally, I use pgadmin3. On the remote server, however, I have no such luxury.

I've already created the backup of the database and copied it over, but is there a way to restore a backup from the command line? I only see things related to GUI or to pg_dumps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

红颜悴 2024-09-06 20:19:40

有两种工具可供查看,具体取决于您创建转储文件的方式。

您的第一个参考来源应该是手册页 pg_dump 因为这就是自己创建转储。它说:

转储可以以脚本或
存档文件格式。脚本转储是
包含 SQL 的纯文本文件
重建所需的命令
将数据库恢复到原来的状态
在它被保存的时候。到
从这样的脚本恢复,将其提供给
psql(1)。可以使用脚本文件
甚至重建数据库
在其他机器和其他
架构;经过一些修改
甚至在其他 SQL 数据库产品上也是如此。

替代存档文件格式
必须与 pg_restore(1) 一起使用
重建数据库。他们允许
pg_restore 对什么有选择性
被恢复,甚至重新排序
恢复之前的项目。这
存档文件格式旨在
可跨架构移植。

所以取决于它被倾倒的方式。如果使用 Linux/Unix,您可能可以使用出色的 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

或者

pg_restore -U username -d dbname -1 filename.dump

查看它们各自的联机帮助页 - 有很多选项会影响恢复的工作方式。在恢复之前,您可能必须清理“实时”数据库或从 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:

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.

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:

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

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.

鹿童谣 2024-09-06 20:19:40

创建备份

pg_dump -h localhost -p 5432 -U postgres -F c -b -v -f 
"/usr/local/backup/10.70.0.61.backup" old_db

-F c自定义格式(已压缩,并且能够与-j N并行执行) -b包括 blob-v详细-f备份文件名

从备份恢复

pg_restore -h localhost -p 5432 -U postgres -d old_db -v 
"/usr/local/backup/10.70.0.61.backup"

设置-h localhost - 选项很重要

create backup

pg_dump -h localhost -p 5432 -U postgres -F c -b -v -f 
"/usr/local/backup/10.70.0.61.backup" old_db

-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

pg_restore -h localhost -p 5432 -U postgres -d old_db -v 
"/usr/local/backup/10.70.0.61.backup"

important to set -h localhost - option

一个人的旅程 2024-09-06 20:19:40

您可能需要以 postgres 身份登录才能拥有数据库的完全权限。

su - postgres
psql -l                      # will list all databases on Postgres cluster

pg_dump/pg_restore

  pg_dump -U username -f backup.dump database_name -Fc 

开关-F指定备份文件的格式:

  • c将使用自定义的PostgreSQL格式,该格式经过压缩并产生最小的备份文件大小
  • < code>d 表示目录,其中每个文件都是一个表
  • t 表示 TAR 存档(大于自定义格式)
  • -h/--host 指定运行服务器的计算机的主机名
  • -W/--password 强制 pg_dump 在之前提示输入密码连接到数据库

恢复备份:

   pg_restore -d database_name -U username -C backup.dump

参数-C应在导入数据之前创建数据库。如果它不起作用,您可以随时创建数据库,例如。使用命令(作为用户 postgres 或有权创建数据库的其他帐户)createdb db_name -O Owner

pg_dump/psql

如果您没有未指定参数 -F 使用默认纯文本 SQL 格式(或使用 -F p)。那么你就不能使用pg_restore。您可以使用psql导入数据。

备份:

pg_dump -U username -f backup.sql database_name

恢复:

psql -d database_name -f backup.sql

You might need to be logged in as postgres in order to have full privileges on databases.

su - postgres
psql -l                      # will list all databases on Postgres cluster

pg_dump/pg_restore

  pg_dump -U username -f backup.dump database_name -Fc 

switch -F specify format of backup file:

  • c will use custom PostgreSQL format which is compressed and results in smallest backup file size
  • d for directory where each file is one table
  • t for TAR archive (bigger than custom format)
  • -h/--host Specifies the host name of the machine on which the server is running
  • -W/--password Force pg_dump to prompt for a password before connecting to a database

restore backup:

   pg_restore -d database_name -U username -C backup.dump

Parameter -C should create database before importing data. If it doesn't work you can always create database eg. with command (as user postgres 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 use pg_restore. You can import data with psql.

backup:

pg_dump -U username -f backup.sql database_name

restore:

psql -d database_name -f backup.sql
柏林苍穹下 2024-09-06 20:19:40

POSTGRESQL 9.1.12

DUMP:

pg_dump -U user db_name > archive_name.sql

输入用户密码并按 Enter。

恢复:

psql -U user db_name < /directory/archive.sql

输入用户密码并按 Enter 键。

POSTGRESQL 9.1.12

DUMP:

pg_dump -U user db_name > archive_name.sql

put the user password and press enter.

RESTORE:

psql -U user db_name < /directory/archive.sql

put the user password and press enter.

总以为 2024-09-06 20:19:40

下面是我用来恢复数据库的 pg_dump 版本:

pg_restore -h localhost -p 5432 -U postgres -d my_new_database my_old_database.backup

或者使用 psql:

psql -h localhost -U postgres -p 5432 my_new_database < my_old_database.backup

其中 -h 主机,-p< /code> 端口,-u 登录用户名,-d 数据库名称

Below is my version of pg_dump which I use to restore the database:

pg_restore -h localhost -p 5432 -U postgres -d my_new_database my_old_database.backup

or use psql:

psql -h localhost -U postgres -p 5432 my_new_database < my_old_database.backup

where -h host, -p port, -u login username, -d name of database

寒尘 2024-09-06 20:19:40

使用 GZIP 备份和恢复

对于较大规模的数据库,这非常好

备份

pg_dump -U user -d mydb | gzip > mydb.pgsql.gz

恢复

gunzip -c mydb.pgsql.gz | psql dbname -U user

https://www.postgresql.org /docs/14/backup-dump.html

Backup and restore with GZIP

For larger size database this is very good

backup

pg_dump -U user -d mydb | gzip > mydb.pgsql.gz

restore

gunzip -c mydb.pgsql.gz | psql dbname -U user

https://www.postgresql.org/docs/14/backup-dump.html

朦胧时间 2024-09-06 20:19:40

这对我有用:

pg_restore --verbose --clean --no-acl --no-owner --host=localhost --dbname=db_name --username=username latest.dump

This worked for me:

pg_restore --verbose --clean --no-acl --no-owner --host=localhost --dbname=db_name --username=username latest.dump
寒冷纷飞旳雪 2024-09-06 20:19:40
Backup:  $ pg_dump -U {user-name} {source_db} -f {dumpfilename.sql}

Restore: $ psql -U {user-name} -d {desintation_db} -f {dumpfilename.sql}
Backup:  $ pg_dump -U {user-name} {source_db} -f {dumpfilename.sql}

Restore: $ psql -U {user-name} -d {desintation_db} -f {dumpfilename.sql}
萌化 2024-09-06 20:19:40

备份与备份恢复

这是我用来备份、删除、创建和恢复我的数据库(在 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将压缩数据库(Format c ustom)
  • 列出 PostgreSQL 用户:sudo -u postgres psql -c "\du+"
  • 您可能需要添加主机名日期./mydb.sql,然后通过以下方式更改:
    ./`主机名`_mydb_`日期 +"%Y%m%d_%H%M"`.sql
    

Backup & Restore

This is the combo I'm using to backup, drop, create and restore my database (on macOS and 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 will compress the database (Format custom)
  • List PostgreSQL users: sudo -u postgres psql -c "\du+"
  • You may want to add hostname and date to ./mydb.sql, then change it by:
    ./`hostname`_mydb_`date +"%Y%m%d_%H%M"`.sql
    
王权女流氓 2024-09-06 20:19:40

试试这个:

psql -U <username> -d <dbname> -f <filename>.sql

从 .sql 文件恢复 DB psql

try this:

psql -U <username> -d <dbname> -f <filename>.sql

Restore DB psql from .sql file

温柔戏命师 2024-09-06 20:19:40

如果您正在使用 docker,这个答案可能会有所帮助。

  1. 启动容器
    docker start ;
    
  2. 访问容器内的 bash
    docker exec -it ;巴什
    
  3. .tar 备份文件复制到 docker 容器(在另一个窗口中)
    docker cp postgres_dump.tar :/
    
  4. 恢复备份
    pg_restore -c -U ; -d <数据库名称> -v“postgres_dump.tar”-W
    
  5. 输入密码

If you are using docker, this answer may be helpful.

  1. Start the container
    docker start <postgres_container_id>
    
  2. Access bash inside container
    docker exec -it <postgres_container_id> bash
    
  3. Copy the .tar backup file to docker container (In another window)
    docker cp postgres_dump.tar <postgres_container_id>:/
    
  4. Restore the backup
    pg_restore -c -U <postgres-user> -d <database-name>  -v "postgres_dump.tar" -W
    
  5. Enter password
对岸观火 2024-09-06 20:19:40

1.打开终端。

2.使用以下命令备份您的数据库:

您的 postgres bin -> /opt/PostgreSQL/9.1/bin/

你的源数据库服务器 -> 192.168.1.111

您的备份文件位置和名称 -> /home/dinesh/db/mydb.backup

你的源数据库名称 -> 我的数据库

/opt/PostgreSQL/9.1/bin/pg_dump --host '192.168.1.111' --port 5432 --username "postgres" --no-password  --format custom --blobs --file "/home/dinesh/db/mydb.backup" "mydatabase"

3。将 mydb.backup 文件恢复到目标位置。

你的目标服务器 -> localhost

你的目标数据库名称 -> mydatabase

创建数据库以恢复备份。

/opt/PostgreSQL/9.1/bin/psql -h 'localhost' -p 5432 -U postgres -c "CREATE DATABASE mydatabase"

恢复备份。

/opt/PostgreSQL/9.1/bin/pg_restore --host 'localhost' --port 5432 --username "postgres" --dbname "mydatabase" --no-password --clean "/home/dinesh/db/mydb.backup"

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

/opt/PostgreSQL/9.1/bin/pg_dump --host '192.168.1.111' --port 5432 --username "postgres" --no-password  --format custom --blobs --file "/home/dinesh/db/mydb.backup" "mydatabase"

3. Restore mydb.backup file into destination.

your destination server -> localhost

your destination database name -> mydatabase

Create database for restore the backup.

/opt/PostgreSQL/9.1/bin/psql -h 'localhost' -p 5432 -U postgres -c "CREATE DATABASE mydatabase"

Restore the backup.

/opt/PostgreSQL/9.1/bin/pg_restore --host 'localhost' --port 5432 --username "postgres" --dbname "mydatabase" --no-password --clean "/home/dinesh/db/mydb.backup"
分分钟 2024-09-06 20:19:40

如果您使用 pg_dump 创建备份,您可以通过以下方式轻松恢复它:

  1. 打开命令行窗口
  2. 转到 Postgres bin 文件夹。例如: cd "C:\ProgramFiles\PostgreSQL\9.5\bin"
  3. 输入恢复数据库的命令。 例如:psql.exe -U postgres -d YourDatabase -f D:\Backup\.sql
  4. 输入 postgres 用户的密码
  5. 检查恢复过程

If you create a backup using pg_dump you can easily restore it in the following way:

  1. Open command line window
  2. Go to Postgres bin folder. For example: cd "C:\ProgramFiles\PostgreSQL\9.5\bin"
  3. Enter the command to restore your database. For example: psql.exe -U postgres -d YourDatabase -f D:\Backup\.sql
  4. Type password for your postgres user
  5. Check the restore process
不奢求什么 2024-09-06 20:19:40

恢复转储文件

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 服务器(Win + R --> services.msc)。

To restore a dump file

psql -d [Dbname] -U [UserName] -p 5432 < [FileLocation]

To restore a .SQL file

pg_restore -U [Username] -d [Dbname] -1 [FileLocation]

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).

心凉怎暖 2024-09-06 20:19:40

1)打开psql终端。

2) 解压/解压转储文件。

3)创建一个空数据库。

4)使用以下命令恢复.dump文件

<database_name>-# \i <path_to_.dump_file>

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

<database_name>-# \i <path_to_.dump_file>
笔芯 2024-09-06 20:19:40

尝试:

pg_restore -h localhost -p 5432 -U <username> -d <dbname> -1 <filename>

try:

pg_restore -h localhost -p 5432 -U <username> -d <dbname> -1 <filename>
千紇 2024-09-06 20:19:40

我没有看到这里提到转储文件扩展名(*.dump)。

这个解决方案对我有用:

我得到了一个转储文件,需要恢复它。

首先,我尝试使用 pg_restore 执行此操作并得到:

pg_restore: error: input file appears to be a text format dump. Please use psql.

我使用 psql 执行此操作并且运行良好:

psql -U myUser -d myDataBase < path_to_the_file/file.dump

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:

pg_restore: error: input file appears to be a text format dump. Please use psql.

I did it with psql and worked well:

psql -U myUser -d myDataBase < path_to_the_file/file.dump
不语却知心 2024-09-06 20:19:40

使用压缩转储保存和恢复完全相同的状态

其他答案分别给出了所有关键位,但希望这将提供您通常想要的“只需保存并恢复到准确状态”命令对。

将数据库转储到文件 mydb.psql

PGPASSWORD=mypassword pg_dump -U my_username -h localhost \
  mydb -Fc -f mydb.psql

从文件 mydb.psql 恢复数据库:

PGPASSWORD=mypassword pg_restore -U my_username -h localhost \
  --clean -d mydb -v --no-privileges --no-owner mydb.psql

一些标志:

  • -Fc:格式“自定义”,它恰好也被压缩而不是纯文本,并生成一个小得多的转储文件。文档: https://www.postgresql.org/docs/current/app- pgdump.html 文件 tmp.psql 说:

    tmp.psql:PostgreSQL 自定义数据库转储 - v1.14-0
    

    还可以使用 -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:

PGPASSWORD=mypassword pg_dump -U my_username -h localhost \
  mydb -Fc -f mydb.psql

Restore the DB from a file mydb.psql:

PGPASSWORD=mypassword pg_restore -U my_username -h localhost \
  --clean -d mydb -v --no-privileges --no-owner 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.html file tmp.psql says:

    tmp.psql: PostgreSQL custom database dump - v1.14-0
    

    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 issuing GRANT 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 exist

PGPASSWORD, -U and -h can of course be modified depending on your login method, e.g. without PGPASSWORD 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.

茶色山野 2024-09-06 20:19:40

恢复 postgres 备份文件取决于您最初如何进行备份。

如果您将 pg_dump 与 -F c 或 -F d 一起使用,则需要使用 pg_restore 否则您可以使用

psql -h localhost -p 5432 -U postgres

备份文件

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

psql -h localhost -p 5432 -U postgres < backupfile

9 ways to backup and restore postgres databases

风柔一江水 2024-09-06 20:19:40

无需密码提示的最短方法

psql "postgresql://<db_user>:<db_pass>@<ip>:<port>/<db_name>" < "backup.sql"

如果您使用的是 Windows 操作系统,则

psql.exe "postgresql://<db_user>:<db_pass>@<ip>:<port>/<db_name>" < "backup.sql"

The shortest way with no password prompt

psql "postgresql://<db_user>:<db_pass>@<ip>:<port>/<db_name>" < "backup.sql"

If you are using Windows OS

psql.exe "postgresql://<db_user>:<db_pass>@<ip>:<port>/<db_name>" < "backup.sql"
找回味觉 2024-09-06 20:19:40

正如下面的链接所述,您可以使用 psql 命令来恢复转储文件:

https://www.postgresql.org/docs/8.1/static/backup.html#BACKUP-DUMP-RESTORE

psql dbname < infile

如果您需要设置用户名,只需在命令后面添加用户名,例如:

psql dbname < infile username

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

psql dbname < infile

if you need to set username just add the username after the command like:

psql dbname < infile username
把时间冻结 2024-09-06 20:19:40

我在运行 pg_dump 时遇到身份验证问题,因此我将转储文件移动

mv database_dump /tmp

到临时目录中,然后运行

su -u postgres
cd /tmp
pg_restore database_dump

如果您有一个大型数据库转储,您可能只想创建另一个目录,当前用户和 postgres 用户可以在其中访问并放置数据库转储文件到其中。

I was having authentication problems running pg_dump, so I moved my dump file

mv database_dump /tmp

into the temp directory and then ran

su -u postgres
cd /tmp
pg_restore database_dump

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.

梦与时光遇 2024-09-06 20:19:40

尝试看看以下命令是否可以帮助您:

sudo su - yourdbuser
psql
\i yourbackupfile

Try to see if the following commands can help you:

sudo su - yourdbuser
psql
\i yourbackupfile
虐人心 2024-09-06 20:19:40

如果您有备份 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 完成其工作。然后您可以检查恢复过程。

If you have a backup SQL file then you can easily Restore it.
Just follow the instructions, given in the below

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 

Type password for your postgres user if needed and let Postgres to do its work. Then you can check the restore process.

ま昔日黯然 2024-09-06 20:19:40

抱歉,死尸帖子,但这些解决方案对我不起作用。我使用的是 postgres 10。在 Linux 上:

  1. 我必须将目录更改为 pg_hba.conf。
  2. 我必须编辑文件以将方法从对等更改为 md5,如此处所述
  3. 重新启动服务: service postgresql-10 restart
  4. 将目录更改为我的 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:

  1. I had to change directory to my pg_hba.conf.
  2. I had to edit the file to change method from peer to md5 as stated here
  3. Restart the service: service postgresql-10 restart
  4. 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.

梦纸 2024-09-06 20:19:40

例如,您可以将用户(角色)johnapple 数据库的架构和数据导出到 backup.sql,使用 < a href="https://www.postgresql.org/docs/current/app-psql.html" rel="nofollow noreferrer">psql 必须用于导入非存档文件,如下所示。 *如果不存在,则创建backup.sql并且我的答案解释了如何导出架构和数据以及我的回答解释了如何将存档backup.sql导入orange数据库和文档解释了如何导出和导入数据库:

pg_dump -U john apple > backup.sql

或者:

pg_dump -U john -s apple > backup.sql

或者,您只导出特定表的数据用户(角色)johnapple 数据库到 backup.sql,仅使用 INSERT 语句,其列名称为如下图:

pg_dump -U john -a --column-inserts -t person -t animal apple > backup.sql

然后,运行上面的命令后需要输入密码:

Password:

现在,您可以将 backup.sql 导入到用户(角色)的 orange 数据库中 john 如下所示。 *您必须事先创建 orange 数据库,否则会出现错误并且 我的回答解释了如何创建数据库:

psql -U john -f backup.sql orange

或:

psql -U john -d orange -f backup.sql

或:

psql --username=john --file=backup.sql orange

或:

psql --username john --file backup.sql orange

或:

psql --username=john --dbname=orange --file=backup.sql

或:

psql --username john --dbname orange --file backup.sql

或者,您可以尝试以下这些在 Windows 上不起作用的方法:

psql -U john orange < backup.sql
psql -U john -d orange < backup.sql
psql --username=john --dbname=orange < backup.sql
psql --username john --dbname orange < backup.sql

然后,您需要运行上述命令后输入密码:

Password for user john:

此外,您可以将backup.sql导入到用户(角色)johnorange数据库中,而无需通过将密码(例如 banana)设置为 PGPASSWORD 如下图:

PGPASSWORD=banana psql -U john -f backup.sql orange

For example, you export the schema and data or only the schema of apple database of the user(role) john to backup.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 archive backup.sql into orange database and the doc explains how to export and import databases:

pg_dump -U john apple > backup.sql

Or:

pg_dump -U john -s apple > backup.sql

Or, you export only the data of the specific tables of apple database of the user(role) john to backup.sql with only INSERT statement which has column names as shown below:

pg_dump -U john -a --column-inserts -t person -t animal apple > backup.sql

Then, you need to input a password after running the command above:

Password:

Now, you can import backup.sql into orange database of the user(role) john as shown below. *You have to create orange database before hand otherwise there is error and my answer explains how to create a database:

psql -U john -f backup.sql orange

Or:

psql -U john -d orange -f backup.sql

Or:

psql --username=john --file=backup.sql orange

Or:

psql --username john --file backup.sql orange

Or:

psql --username=john --dbname=orange --file=backup.sql

Or:

psql --username john --dbname orange --file backup.sql

Or, you can try these below which don't work on Windows:

psql -U john orange < backup.sql
psql -U john -d orange < backup.sql
psql --username=john --dbname=orange < backup.sql
psql --username john --dbname orange < backup.sql

Then, you need to input a password after running the command above:

Password for user john:

In addition, you can import backup.sql into orange database of the user(role) john without a password prompt by setting a password(e.g., banana) to PGPASSWORD as shown below:

PGPASSWORD=banana psql -U john -f backup.sql orange
不打扰别人 2024-09-06 20:19:40

备份==>

选项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

触ぅ动初心 2024-09-06 20:19:40

此解决方案仅适用于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!

岁月苍老的讽刺 2024-09-06 20:19:40

如果您创建了一个名为mydb的新数据库,要使用psql将.sql转储恢复到该数据库,

psql --file=dump.sql --username=postgres --host=localhost --port=5432 mydb

psql将提示输入密码

连接选项是

  -h, --host=HOSTNAME      database server host or socket directory (default: "/var/run/postgresql")
  -p, --port=PORT          database server port (default: "5432")
  -U, --username=USERNAME  database user name (default: "xyz")
  -w, --no-password        never prompt for password
  -W, --password           force password prompt (should happen automatically)

If you have created a new database named mydb, To restore a .sql dump to that database with psql,

psql --file=dump.sql --username=postgres --host=localhost --port=5432 mydb

the password will be prompted by psql

The connection options are

  -h, --host=HOSTNAME      database server host or socket directory (default: "/var/run/postgresql")
  -p, --port=PORT          database server port (default: "5432")
  -U, --username=USERNAME  database user name (default: "xyz")
  -w, --no-password        never prompt for password
  -W, --password           force password prompt (should happen automatically)
夏雨凉 2024-09-06 20:19:40

也许尝试这个命令它会起作用

psql -U postgres -W -d Test2

Maybe try this command it will work

psql -U postgres -W -d Test2 < E:\SampleProject\samplebackup.sql

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文