Reputation: 8873
I am using postgresql 15 and I tried running these:
grant all privileges on database my_database to my_database_user;
grant all privileges on all tables in schema public to my_database_user;
grant all privileges on all sequences in schema public to my_database_user;
grant all privileges on all functions in schema public to my_database_user;
but when I run:
php artisan migrate --seed
I got:
SQLSTATE[42501]: Insufficient privilege: 7 ERROR: permission denied for schema public at character 14 (SQL: create table "migrations" ("id" serial primary key not null, "migration" varchar(255) not null, "batch" integer not null))
What I am missing?
I do make sure .env has correct credentials:
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=my_database
DB_USERNAME=my_database_user
DB_PASSWORD=password
Checking that I did:
postgres=# \du my_database_user
List of roles
Role name | Attributes | Member of
-------------+------------+-----------
my_database_user | | {}
and:
postgres=# SELECT * FROM pg_roles;
rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin | rolreplication | rolconnlimit | rolpassword | rolvaliduntil | rolbypassrls | rolconfig | oid
---------------------------+----------+------------+---------------+-------------+-------------+----------------+--------------+-------------+---------------+--------------+-----------+-------
my_database_user | f | t | f | f | t | f | -1 | ******** | | f | | 16389
Upvotes: 18
Views: 29969
Reputation: 101
For psql scripting newbies like myself, here's oneliner:
PGPASSWORD=<password> psql -h localhost -d <dbname> -U postgres -c "GRANT ALL ON SCHEMA public TO <username>";
Upvotes: 0
Reputation: 246838
You are missing permissions on the schema:
GRANT CREATE ON SCHEMA public TO my_database_user;
You are probably using PostgreSQL v15 or higher. The default permissions on the public
schema have changed in v15. Before, the insecure default was to allow everyone (PUBLIC
) to create objects in schema public
. Now only the database owner can do that, unless you grant extra privileges.
Make sure that you are connected to the correct database when you grant the permissions, as each database has its own schemas.
Upvotes: 32
Reputation: 1070
Do this:
For example:
$ sudo -u postgres psql
postgres=# \c dbname
postgres=# GRANT CREATE ON SCHEMA public TO username;
Upvotes: 14
Reputation: 51
To supplement Laurenz's answer, this is for those who might be executing this from a script, as I was.
Since specifying the database in such a query is not possible (as a prefix, which I initially thought would be logical: <db_name>.<schema_name>
), you first need to connect to the target database (with \c <db_name>
), and then execute the query. If not, it will silently grant creation permission to whichever database context is connected (in my case, it was Postgres).
Upvotes: 5