Skip to main content
trim noise, clarify, format, links
Source Link
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620

DROP DATABASE is a very special command that cannot be undone. To my knowledge there is no way to drop a database inside a transaction. I quote the manual:

DROP DATABASE cannot be executed inside a transaction block.

Whenever you run two When executing multiple commands in a script in pgAdmin, they are automatically wrapped into a transaction. You can explicitly beginbegin and commitcommit transactions, but you cannot send multiplenot with commands at oncethat won't run in one session and "outside" of a transaction context. The manual:

DROP DATABASE cannot be executed inside a transaction block.

Similar in psql when called with -c command,. per documentation:The manual:

If the command string contains multiple SQL commands, they are processed in a single transaction, unless there are explicit BEGIN/COMMIT commands included in the string to divide it into multiple transactions.

However, when fed to psql via standard input:

This is different from the behavior when the same same string is read from a file or fed to psql's standard input [...]
, because then psql sends each [.SQL command separately.

Because of this behavior, putting more than one SQL command in a single -c string often has unexpected results.] either It's better to use repeated -c commands or feed multiple commands to psql's standard input, either using echo as illustrated above, or via a shell here here-document, for example:

psql <<EOF
\x
SELECT * FROM foo;
EOF

So you can use psql with standard input in default autocommit-on modeautocommit-on mode.
Else, you can only run this as a separate commandcommands or wrapped into a BEGIN/COMMIT block separately.

You could use the program dropdb from the shell-command dropdb - or write a shell-script with it that drops multiple database in successiondatabases.

BTW, the only difference between what you can run from pgAdmin and from psqlpsql are the meta-comnmands of psqlmeta-commands of psql - which are not SQL. Those are interpreted by psql and not sent to the database engine. You cannot mixTo switch between meta-commands and SQL. (Well, there are tricks with theuse the separator meta-command \\, there. There is an example in the manual ..example in the manual.)

DROP DATABASE is a very special command that cannot be undone. To my knowledge there is no way to drop a database inside a transaction. I quote the manual:

DROP DATABASE cannot be executed inside a transaction block.

Whenever you run two commands in a script in pgAdmin, they are automatically wrapped into a transaction. You can explicitly begin and commit transactions, but you cannot send multiple commands at once in one session and "outside" of a transaction.

Similar in psql when called with -c command, per documentation:

If the command string contains multiple SQL commands, they are processed in a single transaction, unless there are explicit BEGIN/COMMIT commands included in the string to divide it into multiple transactions.

However, when fed to psql via standard input:

This is different from the behavior when the same string is fed to psql's standard input [...]
[...] either using echo as illustrated above, or via a shell here-document, for example:

psql <<EOF
\x
SELECT * FROM foo;
EOF

So you can use psql with standard input in default autocommit-on mode.
Else, you can only run this as a separate command or wrapped into a BEGIN/COMMIT block separately.

You could use the program dropdb from the shell - or write a shell-script with it that drops multiple database in succession.

BTW, the only difference between what you can run from pgAdmin and from psql are the meta-comnmands of psql - which are not SQL. Those are interpreted by psql and not sent to the database engine. You cannot mix meta-commands and SQL. (Well, there are tricks with the the separator meta-command \\, there is an example in the manual ...)

When executing multiple commands in a script in pgAdmin they are automatically wrapped into a transaction. You can explicitly begin and commit transactions, but not with commands that won't run in a transaction context. The manual:

DROP DATABASE cannot be executed inside a transaction block.

Similar in psql when called with -c command. The manual:

If the command string contains multiple SQL commands, they are processed in a single transaction, unless there are explicit BEGIN/COMMIT commands included in the string to divide it into multiple transactions.

However, when fed to psql via standard input:

This is different from the behavior when the same string is read from a file or fed to psql's standard input, because then psql sends each SQL command separately.

Because of this behavior, putting more than one SQL command in a single -c string often has unexpected results. It's better to use repeated -c commands or feed multiple commands to psql's standard input, either using echo as illustrated above, or via a shell here-document, for example:

psql <<EOF
\x
SELECT * FROM foo;
EOF

So you can use psql with standard input in default autocommit-on mode.
Else, you can only run separate commands.

You could use the shell-command dropdb - or write a shell-script with it that drops multiple databases.

BTW, the only difference between what you can run from pgAdmin and psql are the meta-commands of psql - which are not SQL. Those are interpreted by psql and not the database engine. To switch between meta-commands and SQL, use the separator meta-command \\. There is an example in the manual.

update, was inaccurate
Source Link
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620

DROP DATABASE is a very special command that cannot be undone. To my knowledge there is no way to drop a database inside a transaction. I quote the manual:

DROP DATABASE cannot be executed inside a transaction block.

Whenever you run two commands in a script in pgAdmin, they are automatically wrapped into a transaction. You can explicitly begin and commit transactions, but you cannot run multiplesend multiple commands at once in one session and "outside" of a transaction.

ThereforeSimilar in psql when called with -c command, per documentation:

If the command string contains multiple SQL commands, they are processed in a single transaction, unless there are explicit BEGIN/COMMIT commands included in the string to divide it into multiple transactions.

However, when fed to psql via standard input:

This is different from the behavior when the same string is fed to psql's standard input [...]
[...] either using echo as illustrated above, or via a shell here-document, for example:

psql <<EOF
\x
SELECT * FROM foo;
EOF

So you can use psql with standard input in default autocommit-on mode.
Else, you can only run this as a separate command or wrapped into a BEGIN/COMMIT block separately. 

You could use the program dropdb from the shell - or write a shell-script with it that drops multiple database in succession.

BTW, the only difference between what you can run from pgAdmin and from psql are the meta-comnmands of psql - which are not SQL. Those are interpreted by psql and not sent to the database engine. You cannot mix meta-commands and SQL. (Well, there are tricks with the the separator meta-command \\, there is an example in the manual ...)

DROP DATABASE is a very special command that cannot be undone. To my knowledge there is no way to drop a database inside a transaction. I quote the manual:

DROP DATABASE cannot be executed inside a transaction block.

Whenever you run two commands in a script, they are automatically wrapped into a transaction. You can explicitly begin and commit transactions, but you cannot run multiple commands in one session "outside" of a transaction.

Therefore, you can only run this as a separate command. You could use the program dropdb from the shell - or write a shell-script with it that drops multiple database in succession.

BTW, the only difference between what you can run from pgAdmin and from psql are the meta-comnmands of psql - which are not SQL. Those are interpreted by psql and not sent to the database engine. You cannot mix meta-commands and SQL.

DROP DATABASE is a very special command that cannot be undone. To my knowledge there is no way to drop a database inside a transaction. I quote the manual:

DROP DATABASE cannot be executed inside a transaction block.

Whenever you run two commands in a script in pgAdmin, they are automatically wrapped into a transaction. You can explicitly begin and commit transactions, but you cannot send multiple commands at once in one session and "outside" of a transaction.

Similar in psql when called with -c command, per documentation:

If the command string contains multiple SQL commands, they are processed in a single transaction, unless there are explicit BEGIN/COMMIT commands included in the string to divide it into multiple transactions.

However, when fed to psql via standard input:

This is different from the behavior when the same string is fed to psql's standard input [...]
[...] either using echo as illustrated above, or via a shell here-document, for example:

psql <<EOF
\x
SELECT * FROM foo;
EOF

So you can use psql with standard input in default autocommit-on mode.
Else, you can only run this as a separate command or wrapped into a BEGIN/COMMIT block separately. 

You could use the program dropdb from the shell - or write a shell-script with it that drops multiple database in succession.

BTW, the only difference between what you can run from pgAdmin and from psql are the meta-comnmands of psql - which are not SQL. Those are interpreted by psql and not sent to the database engine. You cannot mix meta-commands and SQL. (Well, there are tricks with the the separator meta-command \\, there is an example in the manual ...)

clarify; deleted 3 characters in body; added 7 characters in body
Source Link
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620

DROP DATABASE is a very special command that cannot be undone. To my knowledge there is no way to drop a database inside a transaction. I quote the manual:

DROP DATABASE cannot be executed inside a transaction block.

It isWhenever you run two commands in a very special command that cannot be undonescript, they are automatically wrapped into a transaction. You can explicitly begin and commit transactions, but you cannot run multiple commands in one session "outside" of a transaction.

Therefore, you can only run this as a separate commandseparate command. You could use the program dropdb from the shell - or write a shell-script with it that drops multiple database in succession.

BTW, the only difference between what you can run from pgAdmin and from psql are the meta-comnmands of psql - which are not SQL. Those are interpreted by psql and not sent to the database engine. You cannot mix meta-commands and SQL.

To my knowledge there is no way to drop a database inside a transaction. I quote the manual:

DROP DATABASE cannot be executed inside a transaction block.

It is a very special command that cannot be undone. You can only run this as a separate command. You could use the program dropdb from the shell - or write a shell-script with it that drops multiple database in succession.

BTW, the only difference between what you can run from pgAdmin and from psql are the meta-comnmands of psql - which are not SQL. Those are interpreted by psql and not sent to the database. You cannot mix meta-commands and SQL.

DROP DATABASE is a very special command that cannot be undone. To my knowledge there is no way to drop a database inside a transaction. I quote the manual:

DROP DATABASE cannot be executed inside a transaction block.

Whenever you run two commands in a script, they are automatically wrapped into a transaction. You can explicitly begin and commit transactions, but you cannot run multiple commands in one session "outside" of a transaction.

Therefore, you can only run this as a separate command. You could use the program dropdb from the shell - or write a shell-script with it that drops multiple database in succession.

BTW, the only difference between what you can run from pgAdmin and from psql are the meta-comnmands of psql - which are not SQL. Those are interpreted by psql and not sent to the database engine. You cannot mix meta-commands and SQL.

Source Link
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620
Loading