HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

PostgreSQL CREATE TABLE creates with incorrect owner

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlownerincorrectcreatescreatewithtable

Problem

I'm using PostgreSQL 9.2.4. When I create a table as a non-superuser in a database owned by that non-superuser, it is owned by the postgres user, so I can't put any data into it unless I explicitly grant myself permission.

I created the non-superuser like this:

admin_user=# create role "test1" NOINHERIT LOGIN ENCRYPTED PASSWORD 'wibble' CONNECTION LIMIT 10;


Then I created a database owned by test1 like this:

admin_user=# create database "test1$db1" with owner "test1";


Then I started a new psql as user test1, and created a table:

test1$db1=> create table test_table (column_name varchar(50));


But I couldn't insert into it:

test1$db1=> insert into test_table values ('some data');                                                                                                    
ERROR:  permission denied for relation test_table


Checking the permissions shows that the table is owned by the postgres user:

test1$db1=> \dt
             List of relations
 Schema |      Name      | Type  |  Owner   
--------+----------------+-------+----------
 public | test_table     | table | postgres


However, I can grant myself permissions and do stuff:

test1$db1=> grant insert, select on test_table to test1;                                                                                                    
GRANT
test1$db1=> insert into test_table values ('some data'); 
INSERT 0 1
test1$db1=> select * from test_table;
 column_name 
-------------
 some data
(1 row)


What's going on? I'm pretty sure this used to work. And the PostgreSQL docs for CREATE TABLE say


CREATE TABLE will create a new, initially empty table in the current
database. The table will be owned by the user issuing the command.

Having to grant permissions to myself on my own tables doesn't sound like it's what I should have to do.

Any help much appreciated!

[UPDATE]

It looks like this might be caused by some kind of change to the PostgreSQL p

Solution

That the owner of your new table turns out to be postgres is very odd.

Either way, you can make Postgres grant or revoke any privileges to / from any role by default with ALTER DEFAULT PRIVILEGES:

ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT INSERT, UPDATE, DELETE ON TABLES TO test1;


This command applies to a given user, or quoting the manual:

If FOR ROLE is omitted, the current role is assumed.

Code Snippets

ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT INSERT, UPDATE, DELETE ON TABLES TO test1;

Context

StackExchange Database Administrators Q#47005, answer score: 6

Revisions (0)

No revisions yet.