You point Laravel at a Postgres schema that does not exist yet, run php artisan migrate, and the command fails before it creates a single table. The error is short and gives you almost nothing to work with:
SQLSTATE[3F000]: Invalid schema name: 7 ERROR: no schema has been selected to create in
This post explains why it happens and the two ways to fix it: a one-line workaround you can use today, and the framework-level fix I built for it.
What actually happens
Laravel keeps track of which migrations have run in a migrations table. Before running any migration, it checks whether that table exists, and creates it if it does not. The problem is where that table gets created. Postgres resolves an unqualified table name against your search_path, and if every schema in the search_path is missing, Postgres has nowhere to put the table. It does not create the schema for you. It errors out.
This usually shows up in two situations. The first is a fresh environment: a new staging server, a new RDS database, or a fresh container where the database exists but the application schema was never created. The second is a multi-tenant setup where each tenant gets its own schema, and that schema is created by application code that has not run yet. In both cases the migration command is fine. The schema it needs to write into simply is not there.
Why CREATE TABLE IF NOT EXISTS does not save you
A reasonable guess is that CREATE TABLE IF NOT EXISTS migrations should handle this. It does not, because the failure is not about the table. The IF NOT EXISTS clause only skips creation when a table with that name already exists. When the schema itself is missing, Postgres cannot even evaluate the statement, so it raises the "no schema has been selected to create in" error before the table check matters.
The same applies to the migrations table check that runs first. Laravel asks Postgres whether the table exists, gets a clean "no", and then tries to create it in a schema that does not exist. The check is correct. The assumption underneath it, that the target schema exists, is what breaks.
The quick fix: create the schema before you migrate
Until the framework handles this, create the schema as a step in your deploy process, before php artisan migrate runs. In psql it is one statement:
CREATE SCHEMA IF NOT EXISTS app;
In a deploy script it looks like this:
psql -h "$DB_HOST" -U "$DB_USERNAME" -d "$DB_DATABASE" -c 'CREATE SCHEMA IF NOT EXISTS app;'
php artisan migrate --force
Make sure the schema name matches the search_path in your Postgres connection config. In Laravel, that lives in config/database.php:
'pgsql' => [
// ...
'search_path' => env('DB_SCHEMA', 'public'),
],
If your search_path lists more than one schema, the first one is the one Postgres writes into, so that is the one that must exist. The IF NOT EXISTS part matters: it makes the step safe to run on every deploy, not just the first one.
If you are debugging a connection and want to see what Postgres is actually using, run SHOW search_path; in psql. If the schemas it lists do not exist in the database, you have found the problem. For more on Postgres connection failures in general, see my earlier post on PostgreSQL "Too Many Clients" on RDS.
The framework-level fix
I hit this while working on Laravel itself, and the right place to fix it is DatabaseMigrationRepository::createRepository(), the method that creates the migrations table. The fix is small and deliberately narrow:
- Only run it for the Postgres driver. Other drivers do not have a search_path, so there is nothing to fix there.
- Read the first schema from the connection's search_path.
- Check
pg_namespaceto see whether that schema exists. - If it does not, run
CREATE SCHEMA IF NOT EXISTSfor it, then continue with creating themigrationstable as before.
Checking pg_namespace first avoids a create statement on every migrate run, and IF NOT EXISTS covers the race where two processes create the schema at the same time. I built this fix against the Laravel framework for issue #62527, with tests covering a missing schema, an existing schema, and a non-Postgres connection that must be left untouched.
How to avoid this in new projects
Three habits keep this error from coming back. First, treat schema creation as infrastructure, not application setup: if your app writes into a schema other than public, your deploy script creates it before the first migration runs. Second, keep search_path explicit in config instead of relying on defaults, so a wrong or missing value fails loudly where you can see it. Third, run php artisan migrate against a truly fresh database in CI, not one that already has your schema, because a working database hides exactly this class of bug.
Takeaway
When Laravel migrations fail on Postgres with "no schema has been selected to create in", the migration code is not broken and your tables are not the problem. The schema in your search_path does not exist yet, and Postgres will not create it for you. Create the schema first, make that step part of every deploy, and the error goes away.