How migrate physical postgres database data to postgres container - postgresql

I have postgres database with stored data.
Now i want to change my application to run in a container and reading from earlier stored data in postgres database.
But how to create volume with already stored data?
A don't want to lose any data. How to achieve it?

Better to use pg_dump https://postgrespro.com/docs/postgresql/9.6/app-pgdump
You need to dump a database to file at first
pg_dump mydb > db.sql
After your container is running you need to restore this data from file as
psql -d newdb -f db.sql

Related

update local Dockerized Postgress DB from an equivalent DB on Azure

I have an un-updated local Postgres server, running on a docker container, now I want to update all new records from Production DB,
which runs on Azure Postgres DB.
I'm aware of the pg_dump as in this Answer
but, I'm not clear where should I commend it - the Azure DB doesn't know the Local one and vice versa?
There are multiple methods which you can try.
The most common and simple approach is to use pg_dump and pg_restore commands in bash to upgrade the database.
In above mentioned method, you first create a dump from the source server using pg_dump. Then you restore that dump file to the target server using pg_restore.
To back up an existing PostgreSQL database, run the following command:
pg_dump -Fc -v --host=<host> --username=<name> --dbname=<database name> -f <database>.dump
Once the file has been created, download it in local environment.
After you've created the target database, you can use the pg_restore command and the --dbname parameter to restore the data into the target database from the dump file.
pg_restore -v --no-owner --host=<server name> --port=<port> --username=<user-name> --dbname=<target database name> <database>.dump
To find more upgrade methods, you can refer https://learn.microsoft.com/en-us/azure/postgresql/how-to-upgrade-using-dump-and-restore#method-1-using-pg_dump-and-psql.
To get more details on pg_dump and pg_restore methood, please refer Microsoft Official document Migrate your PostgreSQL database by using dump and restore.

How to pg_restore one table and its schema from a Postgres dump?

I am having some difficulties with restoring the schema of a table. I dumped my Heroku Postgres db and I used pg_restore to restore one table from it into my local db (it has more than 20 tables). It was successfully restored, but I was having issues when I tried to insert new data into the table.
When I opened up my database using psql, I found out that the restored table is available with all the data, but its schema has zero rows. Is there anyway I could import both the table and its schema from the dump? Thank you very much.
This is how I restored the table into my local db:
pg_restore -U postgres --dbname my_db --table=message latest.dump
Edit:
I tried something like this following the official docs, but it just gets blocked and nothing happened. My db is small, no more than a couple of megabytes and the table's schema I am trying to restore has no more than 100 row.
pg_restore -U postgres --dbname mydb --table=message --schema=message_id_seq latest.dump
As a more general answer (I needed to restore a single table from a huge backup), you may want to take a look at this post: https://thequantitative.medium.com/restoring-individual-tables-from-postgresql-pg-dump-using-pg-restore-options-ef3ce2b41ab6
# run the schema-only restore as root
pg_restore -U postgres --schema-only -d new_db /directory/path/db-dump-name.dump
# Restore per table data using something like
pg_restore -U postgres --data-only -d target-db-name -t table_name /directory/path/dump-name.dump
From the Heroku DevCenter here
Heroku Postgres is integrated directly into the Heroku CLI and offers
many helpful commands that simplify common database tasks
You can check here if your environment is correctly configured.
In this way, you can use the Heroku CLI pg:pull command to pull remote data from a Heroku Postgres database to a local database on your machine.
For example:
$ heroku pg:pull HEROKU_POSTGRESQL_MAGENTA mylocaldb --app sushi

How do I export some data my local Postgres db and import it to a remote without deleting all the data from the remote?

I’m using Postgres 9.5 on Mac Sierra. I want to export some table data from my local machine and import that into a Postgres 9.5 database on a Linux machine. Note I don’t want to blow away the data on the Linux machine, only add the my local machine table rows to the rows that already exist on the tables in the Linux environment (and ignore duplicates). So on my local machine, I ran
pg_dump -U myusername --format custom --section data --inserts --file "t1.backup" --table "table1" --table "table2" --table "addresses" "mydb"
However on my remote machine, when I try and import the file, I get the error
myuser#remote-machine:/tmp$ psql db_production < t1.backup
The input is a PostgreSQL custom-format dump.
Use the pg_restore command-line client to restore this dump to a database.
But I don’t want to use pg_restore because I don’t want to erase the existing table data, I simply want to add to it. How can I achieve this?
Use --format plain instead of custom one. The latter is designed to work exclusively with pg_restore. Plain format also allows you to take a look into dumped data with text editor and verify if that's what you want.
However my quick test shows that it's also possible to append data with pg_restore and custom format data-only dump:
pg_restore -d db_production --data-only t1.backup

Is there a way to repopulate with command-line a postgres db that I can't drop?

I'm looking to load a database from a backup.gz. The backup is raw sql generated from pg_dump -U postgres app_development -f backup.gz -Z9.
I've tried dropping the db with psql -Upostgres -c "drop database app_development" but I get:
ERROR: database "app_development" is being accessed by other users
DETAIL: There are 3 other sessions using the database.
The same thing happens when I use dropdb.
I don't want to dump to a non-ascii version so I don't think I can use pg_restore.
Also, I'm not sure if it helps, but all this is happening in docker.

PostgreSQL - checking if restored database has all the data as original

I backup my PostgreSQL database once a week. One time, during the restore, the restored database was 2.9GB in size, while original database size was 3.7GB. The reason for different sizes is well explained here and that's not a problem.
I back up database using:
pg_dump -U postgres database_name -f backup_file.sql.
To restore the database I use:
psql -U postgres -d target_database -f backup_file.sql.
My question is what is the best way to test and ensure that the restored database has all the data from original database?