Search code examples
postgresqldatabase-restore

Fail to restore a postgres db


i had no problem to restore a postgres database but today when i tried to restore it as before it gave me a error, here whats i have done:

i tried to restore a postgres database as follows:

mostafa@jamareh:~/.../DB$ sudo -u postgres psql -d dbname -f dbname.sql

but it gives me this:

could not change directory to "/home/....../DB"

dbname.sql: No such file or directory

also i tried sudo -su postgres... but it doesn't work neither

what can i do to resolve it ?

thanks in advance,


Solution

  • From an initial look, it could be that the postgres UNIX user does not have access to your dbname.sql file because of permission issues.

    You could either change the permissions (or move the file to a place that is readable by UNIX user postgres, say, /tmp), or, better, restore the database from your own UNIX user, but with the proper database user. For example, you would have:

    mostafa@jamareh:~/.../DB$ psql -d dbname -U postgres -f dbname.sql

    Note that there is no sudo, but that you tell psql to run the script as DB user postgres (assuming that you have the credentials for the postgres DB user).