Error on restore a dockerized PostgreSQL database - postgresql

I have a dockerized Postgres db. I have successfully executed
docker exec -t your-db-container pg_dumpall -c -U postgres > dump_`date +%d-%m-%Y"_"%H_%M_%S`.sql
and generated .sql file. But when I'm trying to restore with the following script:
cat my_dump.sql | docker exec -i your-db-container psql --username="myusername" mydb
I get:
ERROR: syntax error at or near "pg_dumpall"
LINE 1: pg_dumpall: error: could not connect to database "template1"...
What am I missing?

The dump file contains an error message from the backup procedure instead of containing a database dump.
docker exec combines the standard output and the standard error. Its output cannot be trusted for a backup file.
Aside from solving the root problem that pg_dumpall in the container cannot connect to template1, you want a more sophisticated dump procedure that cannot create this situation where a shell error message ends up where SQL statements should be.

Related

Importing existing database into new Dokku application

I have an existing application that I am trying to port over to Dokku, and part of that is getting the existing data store copied over. I am using the Dokku Postgres plugin, but am having trouble getting the database ported over.
In my existing app I am creating a dump of the database:
// Create dump file
pg_dump app_database > db.dump
// Copy over to server hosting Dokku app
scp db.dump sshdetails
// SSH into new server, then attempt to import dump file
dokku postgres:import db < db.dump
When I run the last command, I get the message:
pg_restore: error: input file appears to be a text format dump. Please use psql.
I have tried formatting the dump in a few different formats but no luck. Any advice would be greatly appreciated. Thanks
From the source of dokku postgres:export command in dokku-postgres plugin :
docker exec "$SERVICE_NAME" env PGPASSWORD="$PASSWORD" pg_dump -Fc --no-acl --no-owner -h localhost -U postgres -w "$DATABASE_NAME"
The dokku postgres:export command have -Fc argument for pg_dump which export dump as archive suitable for input into pg_restore (more information into pg_dump man page) and pg_restore command seems to only accept custom format from pg_dump (cf. pg_restore man), so you should use the same -Fc argument in pg_dump command.

Restoring a text `pg_dump` leaves a dangling session

I dump a postgresql database on one machine as follows:
sudo -i -u postgres
pg_dump ire --clean --create --format=p > xfer.sql
On a second, private machine, whose database is not exposed to the network, I restore the dump with:
sudo -i -u postgres
psql -U postgres < xfer.sql
This works once, but then any attempt to overwrite the database gets this error message:
ERROR: database "ire" is being accessed by other users
DETAIL: There is 1 other session using the database.
I can easily fix it using e.g. this solution but how can I avoid creating this dangling session in the first place?

Restore sql file to a postgres container

I'm new with docker and i have to learn at my new job, so i should restore a sql file into a postgres container, when i type the command:
docker exec -i postgres-container pg_restore -U postgres -d postgres /var/lib/postgresql/data/_postgres_2020-11-09T02_00_06Z.sql
i get the following message:
pg_restore: error: input file appears to be a text format dump. Please use psql.
After that i tried to convert that file with psql inside the container but i get this message:
psql: error: FATAL: role "root" does not exist.
How can i get this? can someone help me ?
I got it! activereality's answer solve my problem in this post Backup/Restore a dockerized PostgreSQL database
cat your_dump.sql | docker exec -i your-db-container psql -U postgres -d dbname

Postgres: copy database from one server to another on AWS RDS using PGAdmin

I have 2 postgres database instances on my aws account, basically they both run different engines of postgres and there is no way to downgrade. so I resorted to creating an instance with the engine version that I require and then perform a database copy of all the objects/data. I am trying to use the following command but is throwing an error.
pg_dump -h sourceinstancexx.awsxxx.us-east-2.rds.amazonaws.com -U myUser-W myPass-d myDB | psql -h destinationinstancexx.awsxx.us-east-2.rds.amazonaws.com -d desinationDB -U DestinationUser -W DestinationPW
The error is:
ERROR: syntax error at or near "pg_dump"
LINE 1: pg_dump -h xx.xx.us-east-2.rds.amazonaws.c...
^
SQL state: 42601
Character: 1
You can't run pg_dump as if it was an SQL command. You should use the backup tool provided in PGAdmin, or get access to the system shell and run the pg_dump command from there.

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