Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make pg_dump generate upsert statements in Postgres 9.5

Tags:

postgresql

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

like image 543
Oreoluwa Avatar asked Mar 30 '16 23:03

Oreoluwa


People also ask

How do I create a DDL script in PostgreSQL?

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.

What does pg_dump command do?

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.


1 Answers

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
--
like image 169
filiprem Avatar answered Oct 21 '22 00:10

filiprem