How to extract zip file to tar in postgresql - postgresql

I want to Download DVD Rental Sample Database from http://www.postgresqltutorial.com/postgresql-sample-database/
The database file is in zipformat ( dvdrental.zip) so I need to extract it to dvdrental.tar I have no .tar program in my computer. How can I extract the zip file to dvdrental.tar ?

So, I came across the same problem. The way i fixed it was to download:
"The Unarchiver"
Go to downloads and select your .zip file, open with: "the unachiver.app" (you should now see a .tar file)
Go to your terminal and run this command: pg_restore -U postgres -d dvdrental /Users/username/Downloads/dvdrental.tar
Change username to your username. you might have to change the path depending on where your now .tar file is.
You should now see all the tables

If you use the default Mac unzip program. The dvdrental.tar file will disappear after the unzip finish. Try another unzip app, such as "The Unarchiver". It will leave the dvdrental.tar file in your folder.

If you are trying to restore the dvdrental database use psql. After unarchive the zip file run the below command. Example if you have unarchived to ./downloads folder then
psql -h localhost -U postgres < ./Downloads/dvdrental/restore.sql

Another option:
Download the dvdrental.zip to a directory
from terminal go that directory
then type:
unzip dvdrental.zip
dvdrental.tar should now appear.

For future readers who want to practice with docker psql, here's how I did it.
For overall guide to make psql container, see postgresql docker hub.
Source of sample database: Postgresql tutorial page.
create docker volume
$ docker create volume dvdrental
run docker container
$ docker run --name dvdrental -e POSTGRES_PASSWORD=postgres --restart=always -v dvdrental:/var/lib/postgresql/data/ -p 5433:5432 -d postgres:13
I intentionally binded external port(5433) to internal port(5432), since I'm already using another database instance with 5432 port. If you don't have any postgresql db instance, feel free to change external one to 5432.
copy dvdrental.tar inside the running container
$ docker cp ~/Downloads/dvdrental.tar dvdrental:/dvdrental.tar
Syntax for copy is docker cp <file_name> <container_name>:/<file_name>. Same is true with directory.
I thank Mosd for dropping hints on generating tar file from zip extension.
Create database
$ docker exec -it dvdrental psql -U postgres
psql (13.3 (Debian 13.3-1.pgdg100+1))
Type "help" for help.
postgres=# CREATE DATABASE dvdrental;
CREATE DATABASE
postgres=# exit
I got hint from Postgresql tutorial for creating database in this process.
pg_restore to populate data
$ docker exec -it dvdrental pg_restore -U postgres -d dvdrental /dvdrental.tar
Your dvdrental database is now populated.
You can make connection with tools such as dbeaver with
database name: dvdrental
user: postgres
password: postgres
port: 5433
Remember that your external port is 5433, as you have set above.

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.

Install Postgis in docker container

I created a database with docker using the postgres image as usual
docker run -d \
--name some-postgres \
-e POSTGRES_PASSWORD=mypassword \
-v ${HOME}/postgres-data/:/var/lib/postgresql/data \
-p 5432:5432 \
postgres
now I decided to add a new column in one of the tables to store coordinates using postgis, however when I do
CREATE EXTENSION postgis;
or something similar I get this error message:
ERROR: could not open extension control file "/usr/share/postgresql/12/extension/postgis.control": No such file or directory
is there an additional step one has to take before running the docker container in order to install postgis?
thanks a lot
The postgis extension does not come with vanilla postgres, which does ship with a whole bunch of more general purpose extensions, though nothing notable for geospatial. Take a look at this instead: https://registry.hub.docker.com/r/postgis/postgis/
I know that there is already a response but this could help someone
Firstly you should already have a Postgres image and after a postgres docker container running .
link 2
After you should have the POSTGIS DOCKER IMAGE
[voir ici] : https://hub.docker.com/r/kartoza/postgis/
The image below shows my config after all these steps
After you should stop the running container of postgres which was running at port 5432 for me
this could help: https://blog.eduonix.com/software-development/learn-stop-kill-clean-docker-containers/#:~:text=To%20stop%20a%20container%20you,the%20killing%20is%2010%20seconds.
Now we can create the container that is going to link our postgres container with our postgis Extension
sudo docker run -d --name postgis_postgres -e POSTGRES_PASSWORD=postgrespassword -e POSTGRES_USER=postgres -v /home/judith/Documents/postgres/db-data/:/var/lib/postgresql/data -p 8000:8000 kartoza/postgis:9.6-2.4
Here /home/judith/Documents/postgres/db-data/ is the path to the database data of postgres container
Now we can enter in the running container created at the step 5 with the command
judith#jlas:~$ sudo docker exec -it postgis_postgres bash
root#544c89fadeda:/# //you will be there
Write the command that is going to link to the postgres console admin , here
the 5432 is port where my postgres container was running and postgres is the admin of my postgres in my config , you will config the database admin in the step 1 do not worry .
root#544c89fadeda:/# psql -h localhost -p 5432 -U postgres
After you can create you POSTGIS EXTENSION
postgres=# CREATE EXTENSION postgis;
CREATE EXTENSION
postgres=#

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

How to create Postgres backups from docker container

I have a Postgres 9.5.4 database running inside a docker container. I am using the official Postgres images and I have been trying to create backups from my database, but not luck so far. According docker documentation I was expecting the following command to store the dump.tar file in the /releases/ folder in the host machine (outside the temporal docker container)
docker run -i --rm --link my_postgres:postgres --volume /releases/:/tmp/ postgres:latest pg_dumpall > /tmp/dump.tar
But this throws the following error:
pg_dumpall: could not connect to database "template1": could not connect to server: Connection refused
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Any idea what could be wrong?
Actually I have found the solution
docker run -i --rm --link my_container:postgres -v /releases/:/tmp/ postgres:latest bash -c 'exec pg_dumpall -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres > /tmp/my_backup.tar'
Where Docker arguments:
-i For interactive processes, like a shell, bash, etc.
--rm For deleting the container once the backup is finished
--link For linking this temporal container to my_container, which contains the currently running Postgres database.
-v /releases/:/tmp/ Creates shared volume. The content inside the folder /tmp/ in the container (In this case my_backup.tar) will be automatically visible on folder /releases/ in the host machine.
And bash arguments:
pg_dumpall To export PostgreSQL cluster into a script file.
-h "$POSTGRES_PORT_5432_TCP_ADDR" Obtains the ip address out of the Postgres variable.
-p "$POSTGRES_PORT_5432_TCP_PORT" Obtains the Port out of the Postgres variable (These variables are defined only in the temporal container we just created, as result of the linkage with my_container)
-U The username for connecting to the database

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.