简体   繁体   English

如何使用 psql 创建 Postgres 数据库的 sql 转储文件

[英]How to create an sql dump file of a Postgres database using psql

I currently have created a database via Supabase, which uses the Postgres database.我目前通过 Supabase 创建了一个数据库,它使用 Postgres 数据库。 After doing so, I then downloaded Postgres on my computer, which gave me the psql command line script for use to connect to postgres databases.这样做之后,我在我的计算机上下载了 Postgres,它为我提供了 psql 命令行脚本,用于连接到 postgres 数据库。

I intend to create a local backup of my Supabase database through the psql script.我打算通过 psql 脚本创建我的 Supbase 数据库的本地备份。 This is after I have connected to the supabase database through psql这是在我通过 psql 连接到 supbase 数据库之后

显示 supabase 数据库中的表的命令

From this, i can see that psql successfully connected to the database.从这里,我可以看到 psql 成功连接到数据库。

I then tried to use the pg_dump command to attempt to create a dump file of the database.然后我尝试使用 pg_dump 命令尝试创建数据库的转储文件。 This was the command I used:这是我使用的命令:

pg_dump postgresql://postgres:[Database password]@db.epvzhrganrzfwexmiksg.supabase.co:5432/postgres > dumpfile.sql pg_dump postgresql://postgres:[数据库密码]@db.epvzhrganrzfwexmiksg.supabase.co:5432/postgres > dumpfile.sql

However, after I pressed enter, psql did not run the command, it simply moved to a new command line.但是,在我按下回车后,psql 并没有运行该命令,它只是移动到了一个新的命令行。 As such, I am not sure whether I inputted something wrong for the dump command or is it a different psql command I need to use for it.因此,我不确定我是否为转储命令输入了错误的内容,还是我需要使用不同的 psql 命令。

as mentioned by @adrian-klaver the pg_dump command should be run directly from the terminal, and not from inside the psql shell正如@adrian-klaver 所提到的,pg_dump 命令应该直接从终端运行,而不是从 psql shell 内部运行

run pg_dump -V to check that it's installed.运行pg_dump -V以检查它是否已安装。 It is usually installed when you install psql https://stackoverflow.com/a/49689589/1267728它通常在安装 psql 时安装https://stackoverflow.com/a/49689589/1267728

As mentioned, you have successfully connected with psql.如前所述,您已成功连接 psql。

  • Below are the steps to take plain .sql backup, if you have the OS prompt access.如果您有操作系统提示访问权限,以下是进行普通 .sql 备份的步骤。

From OS prompt从操作系统提示

$ pg_dump mydb > mydb.sql

From psql prompt从 psql 提示符

postgres# \! pg_dump mydb > mydb.sql

Post backup done , mydb.sql file can be copied & import via psql to local machine.备份完成后,可以通过 psql 将 mydb.sql 文件复制并导入到本地计算机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 nodejs 转储 postgres 数据库并与其他 postgres 数据库同步 - How to dump postgres database and sync with other postgres database using nodejs 想要创建一个可以将mysql数据库转储为sql文件的视图 - Want to create a view which can dump mysql database as a sql file postgres 使用 .sql 文件创建数据库 - postgres creating database using .sql file 使用带有[template],[encoding],[owner]和.sql文件的批处理文件创建Postgres数据库 - Create Postgres database using batch file with [template],[encoding],[owner] and a .sql file 使用 mysqldump 时如何创建转储文件 - How to create a dump file when using mysqldump 如何使用psql命令行将参数传递给.sql文件? - How to pass parameters to .sql file using psql command line? 如何 pg_dump RDS Postgres 数据库? - How to pg_dump an RDS Postgres database? 如何将postgres 12生成的sql文件恢复到postgres 9.6数据库中 - How to restore postgres 12 generated sql file into postgres 9.6 database 在不使用密码且不使用sudo -u postgres的情况下运行psql和pg_dump - Run psql and pg_dump without password and without using sudo -u postgres 如何也将JSON文件加载到PSQL数据库 - How to load the JSON file too the PSQL database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM