Server Backup in postgres - postgresql

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.

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

Restore Postgres database using pg_restore over SSH

I have a database server without much disk space, so I took a backup of the entire db (let's just call it redblue) and saved it locally using the following command (I don't have pg running on my computer):
ssh admin#w.x.y.z "pg_dump -U postgres redblue -h localhost " \
>> db_backup_redblue.sql
I'd like to now restore it to another server (1.2.3.4) which contains an older version of "redblue" database - however wanted to ask if this is right before I try it:
ssh admin#1.2.3.4 "pg_restore -U postgres -C redblue" \
<< db_backup_redblue.sql
I wasn't sure if I need to do -C with the name of the db or not?
Will the above command overwrite/restore the remote database with the file I have locally?
Thanks!
No, that will do nothing good.
You have to start pg_restore on the machine where the dump is. Actually, since this is a plain format dump, you have to use psql rather than pg_restore:
psql -h 1.2.3.4 -U postgres -d redblue -f db_backup_redblue.sql
That requires that there is already an empty database redblue on the target system.
If you want to replace an existing database, you have to use the --clean and --create options with pg_dump.
If you want to use SSL, you'll have to configure the PostgreSQL server to accept SSL connections, see the documentation.
I'd recommend the “custom” format of pg_dump.
Of course, you can do this :) Assuming you use ssh keys to authorize user from source host to destination host.
On the source host you do the pg_dump, then pipe through ssh to destination host like this:
pg_dump -C nextcloud | ssh -i .ssh/pg_nextcloud_key postgres#192.168.0.54 psql -d template1
Hope that helps ;)

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

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

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.