POSTRGRESQL: pg_dump - postgresql

Using pg_dump I created a backup database and restored it into a new database. The problem is some columns doesn't have the values displayed. Can I use pg_dump with a where clause to populate the new database with the missing values from the back-up database if the id from the back-up database is find in the new database?

Related

How do I merge a local Postgres dump to AWS RDS without the -c flag?

I have version A of my PostgreSQL database in AWS RDS and version B on my local machine. DB B was created from a dump of DB A and has been updated locally. I am trying to merge the data I have locally to RDS.
A has data that B doesn't have and B has data that A doesn't have, hence I cannot use the -c flag in pg_dump (as in this question).
I export my database with pg_dump:
pg_dump -f dump.sql mydb
I try to import my database to RDS using psql:
psql -h ABC.XYZ.eu-west-2.rds.amazonaws.com -U myself mydb < dump.sql
This updates schema, i.e. adds columns I had locally to RDS, but fails to insert any values into these columns. I get the following error for every table that exists in DB A:
ERROR: duplicate key value violates unique constraint "table_pkey"
As I understand from this question, my sequence may be out of sync, but this seems odd given I get it for every table in DB A, and DB B was created from a dump of DB A.
If I use the -c flag with pg_dump the merge works but all of the data in DB A that DB B did not have gets deleted.
How do I merge my local database into the remote one on AWS without losing data?
If you don't need to modify any of the existing rows, you can use pg_dump's option --on-conflict-do-nothing, new in v12.
This will not add any new columns. pg_dump is not a schema migration tool.

Every time I create a new database it's creating a table in that database

Every time I create a new database it's creating a table in that database. I'm finding information about model databases for Microsoft SQL Server, but I can't find anything for Postgres.
You probably created that table in the template1 database.
When you create a database, Postgres doesn't really create it from scratch, it copies an existing one.
Quote from the manual
By default, the new database will be created by cloning the standard system database template1. A different template can be specified by writing TEMPLATE name.
Just connect to the template1 database and drop the table there.

Clone database and it's tables structure to another database (without data)

I have already known query to clone like:
CREATE DATABASE db_dest WITH TEMPLATE db_src OWNER postgres
But that query also cloned the data in the tables. I want to clone all tables and it's structures in the database, but not the data. How can I do this?
I'm not sure if is SQL comand for this. but you can try
create backoup in PGadmin and in dump option select only schema option and then restore it to new db.

How to recover or recreate orginal Postgres database after dropping it?

I accidentally dropped original postgres database after installing postgreSQL software. Postgres user is still there, \du superuser. How to recover or recreate original postgres database?
https://www.postgresql.org/docs/current/static/sql-createdatabase.html
just run
create database postgres;
it will create db with default template (which is "template1"). If you did not modify it - it will be same is previous "postgres" db
You can only recover it if you have some previous backup of that database, by restoring that backup and then applying WAL files upto the time when drop database statement was issued.
There is no other way to recover a dropped database.

Postgres backup on top of existing database

If I have a backup, I know I can typically do psql dbname < infile
but if I have an existing database and I want to write the backup on top, is there a simple way?
In other words, I have a database that's currently live, and I have a backup. The database crashed and now it only has a few rows, so I want to add my backup rows to the rows currently in the database.