Fail to restore a postgres db - postgresql

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,

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).

Related

How to successfully restore a backup using pg_dump in postgresql?

I am new to Postrgresql. I want to create a backup of a production database and restore it in my development instance. Before I touch production, however, I want to make and restore a backup in development. I figured this would be a trivial effort, but that has not been the case.
I connected to psql using the command below.
sudo -u postgres psql
I ran the following command to create my backup inside psql.
\ pg_dump -U postgres -d dbname > /tmp/kp.bak
I included the "!" with the ""of the command above, but Stackoverflow is having trouble rendering that combination of characters
After that it prompted for a password. When I give it the correct password I get the following error. I reset the password for the PostgreSQL user using the ALTER PASSWORD command, so I know I have the correct password.
FATAL: password authentication failed for user "postgresql"
Since that doesn't work, I configured the pg_hba.conf to not require one and restarted the service. This has had no effect, as I am still prompted for a password when I try to restore. This is the first uncommented line in pg_hba.conf.
local all all trust
Here is the command I am using to do the restore.
\ pg_dump -U postgresql -h localhost -f \tmp\kp.bak dbname;
I am at a loss at how to move forward with this. Can anyone tell me what I am missing?
Thanks
Starting psql just to shell out to pg_dump doesn't make any sense. Just do it directly:
sudo -u postgres pg_dump ....
But once you have moved away from "peer" as your authentication method, there is no point in using the sudo at all, so just do it even more directly:
pg_dump ....
FATAL: password authentication failed for user "postgresql"
I don't find it believable that you specified -U postgres, yet the error message says user "postgresql". Please double check your post for spelling and typing errors.
I eventually got it to work with the following commands
sudo -U postgres psql
Then
\! pg_dump -U postgres -d dbname > /tmp/dbname.bak
Doing this outside of psql did not work. I received the following error despite running with sudo.
bash: /tmp/dbname.bak: Permission denied

Server Backup in postgres

I have to take backup of a remote server and restore it. So, whats the best way to take backup of whole server?
After logging into the root server i have tried:-
[root#server-14 ~]# pg_dumpall > clus.bak
pg_dumpall: could not connect to database "template1": FATAL: role "root" does not exist
So, i logged into the psql prompt with the superuser -U unify
[root#server-14 ~]# psql -U unify
psql (9.3.25)
Type "help" for help.
unify=# pg_dumpall -U unify37 -f ~/tmp/clus.sql
unify-#
But even this is throwing a error. The above two ways didn't get me a backup of server and have no idea what might be the problem .
"pg_dumpall" is run from the OS command prompt, not from inside "psql". It takes many of the same argument as "psql". IF you use "-U unify" for psql, then use it for pg_dumpall as well.
pg_dumpall -U unify > clus.bak
Maybe you want to use unify37 instead, that is something only you can know.

postgresql pg_dumpall password error

postgresql 9.4 following this cheat sheet http://www.postgresonline.com/special_feature.php?sf_name=postgresql90_pg_dumprestore_cheatsheet&outputformat=html trying to backup
here is my command
C:\Program Files\PostgreSQL\9.4\bin>pg_dumpall -h arcserver -U postgres -w -p 5433 -f "R:\Data\LUCZ_2017\PostgreSQL_Backup\lucz.sql"
then it gives me this strange error
pg_dumpall: could not connect to database "template1": fe_sendauth: no password supplied
what is template1? as you can see in the picture there is no template1?
I put in the -w so it doesnt ask me for the password..
what am I doing wrong here?
pg_dumpall will try to dump the entire cluster (all databases that the engine serves). If you need this functionality, you probably only want to dump your lucz database (I'm assuming). Try using the -d option to specify the database to dump.
For help:
pg_dumpall --help
or https://www.postgresql.org/docs/9.4/static/app-pg-dumpall.html

How to run postgresql command from GCE shell?

Installing PostgreSQL on GCE requires root password to run sudo -u postgresql. This prompts for a password, which I was never given.
How do I get this pass, or any way to run postgresql commands from the shell in a different way?
Your system user postgresql doesn't have a password (I state with no proof... but I think you'll find this to be true.)
Normally you should use commands like these:
# Test that YOU can use psql (as postgres) to run a query:
psql -U postgres -c 'select * from pg_catalog.pg_user;'
# Test an interactive session:
psql -U postgres my_database
my_database=# select 42 as the_answer;
# Create a new database
psql -U postgres my_database
my_database=# create database mydb;
Alternatively, it's probably possible to login like this (it usually is):
sudo su postgres
And you could probably use this to run createdb. But running psql as yourself is probably better.
If you want to run commands against your postgresql server you should not need to use sudo, just use this syntax to enter the Postgresql Interactive Shell:
psql -U username database_name
OR
psql -U username hostname database_name
Replacing username with your postgresql usename, hostname (if not running on the same server) with the servers host name and database_name with the name of your database. For example:
psql -U postgressql customers
Normally sudo requires your user account password. So assuming that the account you are running the command from is listed in the sudoers file, the password it is prompting you for should be your own. Have you tried that, as opposed to the root or the postgresql password, which you don't appear to have (or might not even be set).

Can't I get to Postgres with plain psql

I always have to give the command like sudo -u postgres psql in order to login into Postgres console. What do I have to in order to login into postgres like sudo psql or psql
The environment I am working on is Ubuntu Linux 12.04
Thanks in advance.
It's normal that after the installation, only the postgres user is able to do anything with the database server. The installer can't assume that we'd want to open access to anyone else.
To give yourself access as a casual user, assuming as an example that your login name is joe (your normal, non-priviledged user), you just need to create a corresponding user and database:
Inside psql as the postgres administrator (with sudo -u postgres psql), issue:
CREATE USER joe;
CREATE DATABASE joe OWNER joe;
After that, when issuing psql at the shell prompt, it will connect by default to your own database with your username. You no longer have to sudo to postgres until you need to issue other administrator commands.
Your psql is in /usr/bin/psql. You shouldn't need to use sudo unless your permissions are wrong, or unless your link is wrong. (In later versions of PostgreSQL, /usr/bin/psql is a symbolic link to the executable. I don't know whether that's true in 8.4. On my home computer, it links to /usr/share/postgresql-common/pg_wrapper.)
The full skeleton syntax for psql is
psql -U username -h hostname -p portnumber database_name
So, for example, when I connect to my scratch database (named "sandbox"), I do it like this.
$ psql -U postgres -h localhost -p 5432 sandbox
You would substitute
your database username (which must already exist, and which isn't necessarily the same as your network/computer username),
your hostname (but "localhost" is probably right for a local install of PostgreSQL),
the port PostgreSQL is listening on (but 5432 is probably right; it's the default), and
your database name.
Would
psql -U psql
work for you?
EDIT:
I though you would mind about sudo.
If your problem is rather typing -U <user>, you could also set the environment variable PGUSER. This could also be done in your shell's logon script, so that it will always be set.
The other enviroment variables of interest might be PGDATABASE, PGHOST, PGPORT.