2

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
1
  • 2
    you dont run pg_dump inside psql Commented Jun 24, 2020 at 23:50

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Aha, thank you. I did not know pg_dump was a program. Then regarding "/Users/<my_computer>/database_backup.sql" > I was specifying the path on my computer to save my .sql file, is this accurate? I ask because since your answer, I found my "pg_dump" is located here: /usr/local/bin/pg_dump.
Yes whatever is after -f will be where the file is saved. There are plenty of examples at the page I linked to.
Thank you!! This is the command that finally worked: sudo -u postgres pg_dump mydb > db.sql > computer password > postgres password
1

This is the command that finally worked:

sudo -u postgres pg_dump mydb > db.sql 

This prompted me for:

  • my computer password
  • then my postgres password

I did not need to specify any additional paths. The db.sql file automatically saved to my root directory.

2 Comments

Yes the computer user does the sudo and the user postgres does the dump. Just know the the above created a plain text dump file and you will need to use psql not pg_restore to restore it.
Understood on pg_restore, thank you again for your help Adrian.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.