Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pg_dump: [archiver (db)] connection to database "mydb" failed: FATAL: database "mydb" does not exist

ok, following these instructions I've runned:

$ PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump

in the same folder where the database is (app_db.sql) but I keep getting:

pg_dump: [archiver (db)] connection to database "mydb" failed: FATAL:  database "mydb" does not exist

Why is this happening and what can I do solve it?

Thanks

like image 399
Sascuash Avatar asked Oct 05 '22 05:10

Sascuash


1 Answers

You have critically misunderstood how PostgreSQL works.

app_db.sql is not a database, it is a database dump. A sequence of text commands that describe the data in the database that can be replayed to create the database.

It isn't like a Microsoft Access .dbx file or a SQLite3 database file, a database stored in a single file that can be manipulated directly. The only way to work with a PostgreSQL database is via a client/server connection to the PostgreSQL server, which stores the actual database contents in a system dependent location like /var/lib/pgsql which you never need to manipulate directly.

To use the database dump, you must restore it into a new empty database using the psql command, eg:

$ createdb mydb
$ psql -f app_db.sql mydb

This will probably fail with permissions errors if you try to run it exactly as written above. You will need to create yourself a user, give that user ownership of mydb, and possibly edit pg_hba.conf to allow yourself to authenticate depending on your system settings.

A more realistic example for a user with unix login name bob might be:

$ sudo -u postgres createuser bob
$ sudo -u postgres createdb -O bob mydb
$ psql -f dpp_db.sql -1 -v ON_ERROR_STOP=1 mydb 

I strongly recommend that you read the PostgreSQL tutorial and the user manual.

like image 192
Craig Ringer Avatar answered Oct 07 '22 18:10

Craig Ringer