Restore sql file to a postgres container - postgresql

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

Related

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.

Error on restore a dockerized PostgreSQL database

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.

executing a .sql file through ubuntu command line for postgres

I have installed postgresql on ubuntu using:
$ sudo apt install postgresql
Now, I have a series of sql queries I would like to fire to create schemas and users and tables etc. I have put those queries in a .sql file as below:
$ sudo nano postgressetup.sql
CREATE SCHEMA schma;
CREATE USER a2i WITH PASSWORD 'password';
GRANT CONNECT ON DATABASE postgres TO schma;
This file has all the queries. I tried something like:
$ psql -U postgres -d postgres -a -f postgressetup.sql
and received error:
psql: FATAL: Peer authentication failed for user "postgres"
I want to know the way I can execute this .sql file.
Note: I've just installed postgres and no further operation is done on it. Any help is appreciated.
You can use the following command explicitly providing db context user
sudo -u postgres psql -U postgres -d postgres -a -f postgressetup.sql

Loading data from Postgres dump into new db within Docker Postgres Container

I'm moving an application from Heroku into Docker containers, and am trying to figure out how to migrate the data from my Heroku db into my new Postgres container.
Attempt so far
I have copied a dump of the db into the Postgres container.
docker cp latest.dump mycontainer:/latest.dump
However when I try to pg_restore it, I'm getting errors as I attempt to run a docker exec command.
docker exec <pg_container> pg_restore -d <db_name> latest.dump
pg_restore: [archiver (db)] connection to database "<db_name>" failed: FATAL: role "root" does not exist
Or when I try to run with the db user:
docker exec <pg_container> -u <db_user> pg_restore -d <db_name> latest.dump
OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: \"-u\": executable file not found in $PATH": unknown
Question
What is the correct way to go about loading this data into the new database within the docker container?
I was able to find the solution as a one line command:
docker exec <container_name> pg_restore --verbose --clean --no-acl --no-owner -h postgres --dbname=postgresql://<user>:<password>#127.0.0.1:<port>/<db_name> latest.dump
latest.dump is the name of my db dump from Heroku so that can be changed to whatever the name of your dump is.

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