I want to dump a database and restore dump on another database that could have data in it. How do I make pg_dump
generate insert on conflict update
statements instead of just insert statements? I use Postgres 9.5
Using PostgreSQL executablewith -s (–schema-only) and -t (–table) option to generate DDL of required tables. The option -s dictates that only DDL has to be generated and the option -t dictates that we want DDL of only tables to be generated.
pg_dump is a utility for backing up a PostgreSQL database. It makes consistent backups even if the database is being used concurrently. pg_dump does not block other users accessing the database (readers or writers). pg_dump only dumps a single database.
With PostgreSQL 12 and later, you can use --on-conflict-do-nothing
switch of pg_dump
. See docs.
Example:
$ pg_dump test --table=lifehack --section=data --column-inserts --on-conflict-do-nothing
[...]
--
-- Data for Name: lifehack; Type: TABLE DATA; Schema: public; Owner: filip
--
INSERT INTO public.lifehack (id, content) VALUES (1, 'Brush your teeth twice a day.') ON CONFLICT DO NOTHING;
--
-- PostgreSQL database dump complete
--
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With