在 macOS 上通过 psql 创建 PostgreSQL 数据库备份 [英] Creating a PostgreSQL database backup via psql on macOS

查看:71
本文介绍了在 macOS 上通过 psql 创建 PostgreSQL 数据库备份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 psql 通过 macOS 上的终端创建 PostgreSQL 本地 db 转储.我的下一步是上传/导入我的整个数据库到我的 Amazon RDS PostgreSQL 数据库实例.

谁能帮助我使用终端命令为我的mydb"数据库创建转储文件mydbdump.sql".我已经对现有问题进行了排序,搜索了论坛(尝试了类似的堆栈溢出问题),甚至 Amazon RDS 文档(上面,链接),并且我对这些文档中的任何一个命令都不走运.我做错了什么?

以下是我迄今为止所尝试的(我没有得到任何回报,甚至没有错误):

通过sudo:

sudo -u postgres psql pg_dump -Fc -o -f/Users//database_backup.sql mydb

通过psql,连接到mydb(\c mydb;):

mydb-# pg_dump dbname=mydb -f mydbdump.sql

mydb-# pg_dump -U postgres -W -F t mydb >/Users//database_backup.sql

mydb-# pg_dump -Fc mydb >/Users//database_backup.sql

mydb-# pg_dump -U postgres mydb >database_backup.sql

mydb-# pg_dump -Fc mydb >/Users//database_backup.sql

mydb-# pg_dump -U postgres -W -F t mydb >/Users//database_backup.sql

供参考:我在 settings.py 中使用 Django:

DATABASES = {'默认': {'引擎':'django.db.backends.postgresql_psycopg2','NAME': 'mydb','USER': 'dbuser','密码': '<我的密码>','HOST': '本地主机','港口': '',}}

顺便说一下,我的 Django 应用运行成功了.

当我列出 mydb 中的所有数据库关系时:

\dt mydb;

输出:

关系列表架构 |姓名 |类型 |所有者--------+----------------------------+-------+--------公共|auth_group |表|数据库用户公共|auth_group_permissions |表|数据库用户公共|auth_permission |表|数据库用户公共|auth_user |表|数据库用户公共|auth_user_groups |表|数据库用户公共|auth_user_user_permissions |表|数据库用户公共|django_admin_log |表|数据库用户公共|django_content_type |表|数据库用户公共|django_migrations |表|数据库用户公共|django_session |表|数据库用户公共|polls_choice |表|数据库用户公共|polls_question |表|数据库用户(12 行)

当我在 psql 中列出所有模式时:

选择 s.nspname 作为 table_schema,s.oid 作为 schema_id,u.usename 作为所有者来自 pg_catalog.pg_namespace s在 u.usesysid = s.nspowner 上加入 pg_catalog.pg_user u按 table_schema 排序;

输出:

table_schema |schema_id |所有者--------------------+------------+------------information_schema |13337 |邮局pg_catalog |11 |邮局pg_temp_1 |12314 |邮局pg_toast |99 |邮局pg_toast_temp_1 |12315 |邮局公共|2200 |邮局

解决方案

第一次在 psql 中运行 pg_dump 将不起作用.

其次:

sudo -u postgres psql pg_dump -Fc -o -f/Users//database_backup.sql mydb

是第一件事的变体.你想要:

<预><代码>sudo -u postgres pg_dump -Fc -f/Users//database_backup.sql mydb

pg_dump 本身就是一个客户端程序.

注意:没有 -o.有 -O 用于无所有者.

查看本页底部的示例:

https://www.postgresql.org/docs/current/app-pgdump.html

最后你确定这不会让你出错吗?:

test(5432)=# pg_dump dbname=mydb -f mydbdump.sql;错误:pg_dump"处或附近的语法错误;第 1 行:pg_dump dbname=mydb -f mydbdump.sql;

如果没有,我会在 postgresql.conf 中检查您的日志设置.

I'm trying to create a PostgreSQL local db dump via Terminal on macOS using psql. My next step is to upload/import my entire db to my Amazon RDS PostgreSQL db instance.

Can anyone help me with the Terminal command to create a dump file 'mydbdump.sql' for my 'mydb' database. I've sorted through existing questions, searched forums (tried similar Stack Overflow question), even Amazon RDS docs (above, link), and am having no luck with the commands from any of these docs. What am I doing wrong?

Here is what I've tried so far (and I've gotten nothing back, not even an error):

Via sudo:

sudo -u postgres psql pg_dump -Fc -o -f /Users/<my_computer>/database_backup.sql mydb

Via psql, connected to mydb (\c mydb;):

mydb-# pg_dump dbname=mydb -f mydbdump.sql

mydb-# pg_dump -U postgres -W -F t mydb > /Users/<my_computer>/database_backup.sql

mydb-# pg_dump -Fc mydb > /Users/<my_computer>/database_backup.sql

mydb-# pg_dump -U postgres mydb > database_backup.sql

mydb-# pg_dump -Fc mydb > /Users/<my_computer>/database_backup.sql

mydb-# pg_dump -U postgres -W -F t mydb > /Users/<my_computer>/database_backup.sql

For reference: I'm using Django, in settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydb',
        'USER': 'dbuser',
        'PASSWORD': '<MYPASSWORD>',
        'HOST': 'localhost',
        'PORT': '',
    }
}

By the way, my Django app is running successfully.

When I list all database relations in mydb:

\dt mydb;

Output:

List of relations
 Schema |            Name            | Type  | Owner  
--------+----------------------------+-------+--------
 public | auth_group                 | table | dbuser
 public | auth_group_permissions     | table | dbuser
 public | auth_permission            | table | dbuser
 public | auth_user                  | table | dbuser
 public | auth_user_groups           | table | dbuser
 public | auth_user_user_permissions | table | dbuser
 public | django_admin_log           | table | dbuser
 public | django_content_type        | table | dbuser
 public | django_migrations          | table | dbuser
 public | django_session             | table | dbuser
 public | polls_choice               | table | dbuser
 public | polls_question             | table | dbuser
(12 rows)

When I list all schemas in psql:

select s.nspname as table_schema,
       s.oid as schema_id,  
       u.usename as owner
from pg_catalog.pg_namespace s
join pg_catalog.pg_user u on u.usesysid = s.nspowner
order by table_schema;

Output:

table_schema    | schema_id |  owner   
--------------------+-----------+----------
 information_schema |     13337 | postgres
 pg_catalog         |        11 | postgres
 pg_temp_1          |     12314 | postgres
 pg_toast           |        99 | postgres
 pg_toast_temp_1    |     12315 | postgres
 public             |      2200 | postgres

解决方案

First running pg_dump inside psql will not work.

Second, this:

sudo -u postgres psql pg_dump -Fc -o -f /Users/<my_computer>/database_backup.sql mydb

is a variation of the first thing. You want:


sudo -u postgres pg_dump -Fc -f /Users/<my_computer>/database_backup.sql mydb

pg_dump is a client program by itself.

NOTE: No -o. There is -O which is for no-owner.

See examples at bottom of this page:

https://www.postgresql.org/docs/current/app-pgdump.html

Lastly are you sure this doesn't get you an error?:

test(5432)=# pg_dump dbname=mydb -f mydbdump.sql;
ERROR:  syntax error at or near "pg_dump"
LINE 1: pg_dump dbname=mydb -f mydbdump.sql;

If not then I would check your logging settings in postgresql.conf.

这篇关于在 macOS 上通过 psql 创建 PostgreSQL 数据库备份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆