Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR: cannot execute CREATE TABLE in a read-only transaction

Tags:

postgresql

I'm trying to setup the pgexercises data in my local machine. When I run: psql -U <username> -f clubdata.sql -d postgres -x I get the error: psql:clubdata.sql:6: ERROR: cannot execute CREATE SCHEMA in a read-only transaction.

Why did it create a read-only database on my local machine? Can I change this?

like image 902
ltrainpr Avatar asked Jul 02 '15 13:07

ltrainpr


People also ask

How do you check if Postgres is read-only?

In mysql we can check whether the database is in read-only mode by triggering the below query. SELECT schemaname||'. '||tablename FROM pg_tables WHERE has_table_privilege ( 'postgres', schemaname||'. '||tablename, 'select' ) AND schemaname NOT IN ( 'pg_catalog','information_schema');

How do I make a table read-only in PostgreSQL?

create table procedure ( id int primary key not null, name varchar(50) not null ); insert into procedure (id, name) values (1, 'Send Exam Request'); insert into procedure (id, name) values (2, 'Confirm Exam Data'); insert into procedure (id, name) values (3, 'Close Visit'); -- Now, make the table read-only.

What is read-only transaction?

A read-only transaction or query is a transaction which does not modify any data.

How do I show all databases in PostgreSQL?

Use \l or \l+ in psql to show all databases in the current PostgreSQL server. Use the SELECT statement to query data from the pg_database to get all databases.


3 Answers

Normally the most plausible reasons for this kind of error are :

  • trying create statements on a read-only replica (the entire instance is read-only).

  • <username> has default_transaction_read_only set to ON

  • the database has default_transaction_read_only set to ON

The script mentioned has in its first lines:

CREATE DATABASE exercises;
\c exercises
CREATE SCHEMA cd;

and you report that the error happens with CREATE SCHEMA at line 6, not before.

That means that the CREATE DATABASE does work, when run by <username>. And it wouldn't work if any of the reasons above was directly applicable.

One possibility that would technically explain this would be that default_transaction_read_only would be ON in the postgresql.conf file, and set to OFF for the database postgres, the one that the invocation of psql connects to, through an ALTER DATABASE statement that supersedes the configuration file.

That would be why CREATE DATABASE works, but then as soon as it connects to a different database with \c, the default_transaction_read_only setting of the session would flip to ON.

But of course that would be a pretty weird and unusual configuration.

like image 66
Daniel Vérité Avatar answered Oct 21 '22 20:10

Daniel Vérité


Reached out to pgexercises.com and they were able to help me.

I ran these commands(separately):

psql -U <username> -d postgres
begin;
set transaction read write;
alter database exercises set default_transaction_read_only = off;
commit;
\q

Then I dropped the database from the terminal dropdb exercises and ran script again psql -U <username> -f clubdata.sql -d postgres -x -q

like image 24
ltrainpr Avatar answered Oct 21 '22 19:10

ltrainpr


I was having getting cannot execute CREATE TABLE in a read-only transaction, cannot execute DELETE TABLE in a read-only transaction and others.

They all followed a cannot execute INSERT in a read-only transaction. It was like the connection had switched itself over to read-only in the middle of my batch processing.

Turns out, I was running out of storage!

Write access was disabled when the database could no longer write anything. I am using Postgres on Azure. I don't know if the same effect would happen if I was on a dedicated server.

like image 22
Brad Mathews Avatar answered Oct 21 '22 19:10

Brad Mathews