Skip to content

Instantly share code, notes, and snippets.

@ValentinMouret
Created July 6, 2022 06:49
Show Gist options
  • Save ValentinMouret/71287ca5eb2cc150c11213a39d32289a to your computer and use it in GitHub Desktop.
Save ValentinMouret/71287ca5eb2cc150c11213a39d32289a to your computer and use it in GitHub Desktop.
cat /Users/valentinmouret/Desktop/users.csv \
| psql -c "
begin;
create temp table batch_user (
id text primary key,
document jsonb not null
);
copy batch_user (id, document)
-- We can run the same command but reading from standard input
-- intead of a file on the file system.
from STDIN
with (format 'csv',
header,
delimiter ',');
insert into user
(id, email)
select batch_user.id,
batch_user.document->>'email' as email
from batch_user
left join user
using (id)
where user.id is null
and batch_user.document->>'email' is not null;
commit;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment