pg_dump 0 bytes backup file, stuck after saving database definition - postgresql

I am trying to backup Postgres database but I always get 0 bytes backup size. If I use the verbose switch, I can see that it is stuck at
pg_dump: saving database definition
I have tried taking backup of specific table and it works fine. Below is my command:
pg_dump -U backupuser -p 5432 -Ft -v -d database > database.tar

I was able to resolve this. When I was creating the backup, I checked the postgres logs and it was clearly written there that during backup the server wasn't reachable. Now to resolve this, I have added a command to restart the postgres before the scheduled backup and it is working fine now.

Related

unexpected data offset flag 137 when restoring database using pg_restore

I'm trying to restore a large database (14G) that was backed up using pg_dump.
I'm using this command to restore it.
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U username -d dbname backupfile.dump
I have tried several other commands (using pg_restore) but I got the same error message.
Here is the error message:
pg_restore: error: unexpected data offset flag 137
Has anyone have any idea what was wrong? I haven't been able to find the meaning of the flag 137 ont the internet.
Note: I downloaded the database from server using scp, I'm quite sure the file has been downloaded completely. The database was backed up using pg_dump with postgresql version 13 (ubuntu), and I'm trying to restore it in my local (macOS) using posgresql 14. Could it be the postgresql version? I haven't encountered such problem before.

Copying Postgresql DB dump from remote server to local

I want to take a DB dump from a remote server and then copy this dump to my local.
I tried couple of commands but didn't worked.
Lastly I tried the command below;
pg_dump -h 10.10.10.70 -p 5432 -U postgres -d mydb | gzip > db1.gz
I succesffully take the DB and tried with restore from Pgadmin, it gives;
pg_restore: error: input file appears to be a text format dump. Please use psql
But at this point I can't use psql, I have to use Pgadmin and not sure if I'm able to get successfully DB dump to my local. I mean I can't verify with restore.
How can I take DB dump from remote server to my local?
Thanks!
Use the "custom" format:
pg_dump -F c -h 10.10.10.70 -p 5432 -U postgres -f mydb.dmp mydb
That can be restores with pg_restore and hence with pgAdmin.
You do not have to use pgAdmin. pgAdmin uses pg_restore, and there is nothing that keeps you from using it too.

Restore pg_dump with pgAdmin

i'm trying to restore a pg_dump with pgAdmin 4.
this is the command i've used to create the backup pg_dump -U {dbUser} -h localhost -Fc {dbName} > {backupFolder}/{backupName}, but when i try to restore it using pgAdmin i get this:
If i execute the command pg_restore manually i get this error
pg_restore: error: could not read from input file: end of file
I've created the dump on a linux machine and trying to restore it with pgAdmin from a windows machine to the linux machine
any help? thanks!
The problem was actually the ftp method i was using to transfer the dump... The file was a bit bigger after the ftp transfer, fixing the ftp transfer have solved the problem!

Dump Postgres database when space is tight?

Imagine this situation. I have a server that has only 1 GB of usable space. A Postgres database takes about 600MB of that (as per SELECT pg_size_pretty(pg_database_size('dbname'));), and other stuff another 300MB, so I have only 100 MB free space.
I want to take a dump of this database (to move to another server).
Naturally a simple solution of pg_dump dbname > dump fails with a Quota exceeded error.
I tried to condense it first with VACUUM FULL (not sure if it would help for the dump size, but anyway), but it failed because of disk limitation as well.
I have SSH access to this server. So I was wondering: is there a way to pipe the output of pg_dump over ssh so that it would be output to my home machine?
(Ubuntu is installed both on the server and on the local machine.)
Other suggestions are welcome as well.
Of course there is.
On your local machine do something like:
ssh -L15432:127.0.0.1:5432 user#remote-machine
Then on your local machine you can do something like:
pg_dump -h localhost -p 15432 ...
This sets up a tunnel from port 15432 on your local box to 5432 on the remote one. Assuming permissions etc allow you to connect, you are good to go.
(if the machine is connected to a network) you can do everything from remote, given sufficient authorisation:
from your local machine:
pg_dump -h source_machine -U user_id the_database_name >>the_output.dmp
And you can even pipe it straight into your local machine (after taking care of user roles and creation of DBs, etc):
pg_dump -h ${ORIG_HOST} -U ${ORIG_USER} -d ${ORIG_DB} \
-Fc --create | pg_restore -c -C | psql -U postgres template1
pg_dump executes on the local (new) machine
but it connects to the $ORIG_HOST as user $ORIG_USER to db $ORIG_DB
pg_restore also runs on the local machine
pg_restore is not really needed (here) but can come in handy to drop/rename/create databases, etc
psql runs on the local machine, it accepts a stream of SQL and data from the pipe, and executes/inserts it to the (newly created) database
the connection to template1 is just a stub, because psql insists on being called with a database name
if you want to build a command-pipe like this, you should probably start by replacing the stuff after one of the | pipes by more or less, or redirect it to a file.
You might need to import system-wide things (usernames and tablespaces) first

How to restore PostgreSQL database backup without psql and pg_restore?

I am using postgresql on an embedded device running yocto (customized linux). The package containing postgresql does not provide psql or pg_restore. /usr/bin provides the following tools:
pg_basebackup indicates that I am able to create backups. But how am I supposed to restore a backup within a terminal? With pg_restore or psql this would not be a problem for me.
Please note: I want to use a backup created on my windows/linux computer to create the initial database on the embedded device.
Create backup of the db using command:
pg_basebackup -h {host_ip} -D pg_backup -U {USER}
for restoring just change the data folder to pg_backup. Now start the psql server.