Shashi
Shashi

Reputation: 25

does "\copy" command handle commit and rollback in postgres?

I am trying execute .sql file with psql. I run .sql file inside i have written following query \copy table name from .dumb. So if the command fails, will it handle commit/rollback by default. or we need to take care of that.

Upvotes: 1

Views: 4023

Answers (2)

JosMac
JosMac

Reputation: 2322

All depends on version of PG you are running. Because default for psql is autocommit on. And in newer versions you cannot set it off. So every successful manually issued COPY or \copy command is immediately commited.

Upvotes: 1

Vao Tsun
Vao Tsun

Reputation: 51609

If \copy fails transaction will be aborted, here is example:

t=# \! cat s07
create table trans(i int);
copy s07 from '/no such file';
t=# begin;
BEGIN
t=# \i s07
CREATE TABLE
psql:s07:2: ERROR:  could not open file "/no such file" for reading: No such file or directory
t=# select * from trans;
ERROR:  current transaction is aborted, commands ignored until end of transaction block
t=# end;
ROLLBACK

Upvotes: 1

Related Questions