Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

339
Vistas
Can I use Laravel's Schema::createDatabase() without first selecting a DB?

I have a little bit of a chicken and egg problem here. I am building a multitenant app in Laravel where each tenant has their own database on a Postgrsql server. So I have a job to create a new tenant that calls:

Schema::connection($this->tenant->getDatabaseConnection())->createDatabase($this->tenant->getDatabaseName())

So $this->tenant->getDatabaseConnection() chooses a tenant DB server to add the tenant to and returns the that's servers connection name. Then $this->tenant->getDatabaseName() will return the string of the DB name for that tenant.

I got one small problem. This throws an exception:

SQLSTATE[08006] [7] FATAL: database "port=5432" does not exist (SQL: create database "local_tenant_(id)" encoding "utf8")

This is because the connection by default had the DB name as null. And when a user logs into their tenant we dynamically inject getDatabaseName() from the Tenant model into that.

But I can't do that here because the tenant DB does not yet exist. So here are the options I see:

  1. choose a random database of an existing tenant on the server to "act as" when creating the new tenant's DB. I don't really like having to hijack another tenant's DB even temporarily like that even though this should have 0 side affects to that tenant. And if it's the first tenant being provisioned on the server, there's no existing DB to use, which makes this a no-go in my book on it's own.

  2. I could just use the DB providers API to create the tenant DB. This would be by far the simplest but would break in local and testing environments and therefore I want to avoid.

  3. I could just bypass Laravel and establish a direct PDO connection to the database for this, but I would say that's my last resort if I can't find something better.

Technically you don't have to have a DB selected to use the create DB command. That's an idiosyncrasy of Laravel's Schema Builder that it tries to select a DB before calling the method (in this case CreatedDatabase). Is there a way around this, so I can create the DB without Laravel first trying to select a null DB?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The correct solution is to connect to the postgres database in order to run CREATE DATABASE. The postgres database is always there (unless somebody dropped it), and that is what it is designed for.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda