Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

angelorigo's avatar

Postgresql permissions

Hi,

How can i fix the permission to the postgresql user so the tables can be created when running php artisan migrate ?

This is the error message : Illuminate\Database\QueryException

SQLSTATE[42501]: Insufficient privilege: 7 ERROR: permission denied for schema public at character 14 (Connection: pgsql, SQL: create table "migrations" ("id" serial not null primary key, "migration" varchar(255) not null, "batch" integer not null))

I do create an user into postgresql an run: GRANT USAGE, CREATE ON SCHEMA public TO myuser; and grant all privileges on database MYDATABASE to MYUSER;

0 likes
2 replies
LaryAI's avatar
Level 58

To resolve the permission issue you're encountering with PostgreSQL when running php artisan migrate, you need to ensure that the user has the appropriate privileges on the schema and the database. Here's a step-by-step guide to fix this:

  1. Connect to PostgreSQL: First, connect to your PostgreSQL database using a superuser account (like postgres).

    psql -U postgres
    
  2. Grant Privileges on the Schema: Ensure that your user has the necessary privileges on the public schema. You can do this by running the following command:

    GRANT USAGE, CREATE ON SCHEMA public TO myuser;
    
  3. Grant Privileges on the Database: Make sure your user has all privileges on the database. You can grant these privileges with:

    GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;
    
  4. Grant Privileges on Existing Tables: If there are existing tables and you want your user to have access to them, you need to grant privileges on those tables as well:

    GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO myuser;
    
  5. Grant Privileges on Sequences: If your tables use sequences (like serial columns), you should also grant privileges on all sequences:

    GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO myuser;
    
  6. Check the Connection Configuration: Ensure that your Laravel application is using the correct database credentials in the .env file:

    DB_CONNECTION=pgsql
    DB_HOST=127.0.0.1
    DB_PORT=5432
    DB_DATABASE=mydatabase
    DB_USERNAME=myuser
    DB_PASSWORD=mypassword
    
  7. Run Migrations: After setting the correct permissions and ensuring your configuration is correct, try running your migrations again:

    php artisan migrate
    

By following these steps, you should be able to resolve the permission issue and successfully run your migrations. If you continue to experience issues, double-check the user and database names, and ensure there are no typos in your commands or configuration files.

11_10's avatar

@angelorigo To fix the permission issue when running php artisan migrate, you need to make sure the PostgreSQL user has the proper rights to create tables and modify the database. It seems that your user doesn’t have enough permissions to perform actions in the public schema. You should grant the user access to connect to the database, use the public schema, create tables, and have full privileges on the tables and sequences within that schema. This will allow the migration command to run without errors and create the necessary tables.

This is similar to how, in Clash of Clans, you need to ensure your troops have enough resources and upgrades to perform effectively. If they lack the necessary upgrades (permissions), they won’t be able to carry out the task (like attacking or defending). Similarly, your database user needs the right permissions to execute database changes.

Please or to participate in this conversation.