PG cannot execute UPDATE in a read-only transaction | How to add column if not exists on PostgreSQL
时间: 2023-11-22 14:52:51 浏览: 181
The error message "PG cannot execute UPDATE in a read-only transaction" usually occurs when you try to modify data in a read-only transaction. To fix this, you need to run the UPDATE query in a transaction that has write access.
Regarding adding a column if it does not exist, you can use the ALTER TABLE statement with the ADD COLUMN clause and the IF NOT EXISTS option. Here is an example:
```
ALTER TABLE mytable
ADD COLUMN IF NOT EXISTS new_column_name data_type;
```
This will add a new column to the "mytable" table only if it does not already exist. Replace "mytable" with the name of your table and "new_column_name" with the name of the new column you want to add. Replace "data_type" with the appropriate data type for the new column.
阅读全文