{"id":"2500ac282ca11f8c","repo":"laravel/framework","slug":"invalid-database-connection-db","errorCode":null,"errorMessage":"Invalid database connection [{$db}].","messagePattern":"Invalid database connection \\[(.+?)\\]\\.","errorType":"exception","errorClass":"UnexpectedValueException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Console/DbCommand.php","lineNumber":85,"sourceCode":"\n        return self::SUCCESS;\n    }\n\n    /**\n     * Get the database connection configuration.\n     *\n     * @return array\n     *\n     * @throws \\UnexpectedValueException\n     */\n    public function getConnection()\n    {\n        $connection = $this->laravel['config']['database.connections.'.\n            (($db = $this->argument('connection')) ?? $this->laravel['config']['database.default'])\n        ];\n\n        if (empty($connection)) {\n            throw new UnexpectedValueException(\"Invalid database connection [{$db}].\");\n        }\n\n        if (! empty($connection['url'])) {\n            $connection = (new ConfigurationUrlParser)->parseConfiguration($connection);\n        }\n\n        if ($this->option('read')) {\n            $connection = $this->mergeConnectionConfiguration($connection, 'read');\n        } elseif ($this->option('write')) {\n            $connection = $this->mergeConnectionConfiguration($connection, 'write');\n        } elseif (! $this->option('pooled') && ($connection['driver'] ?? null) === 'pgsql' && ($connection['pooled'] ?? false) === true && ! empty($connection['direct'])) {\n            $connection = $this->mergeConnectionConfiguration($connection, 'direct');\n        }\n\n        return $connection;\n    }\n\n    /**","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Console/DbCommand.php#L67-L103","documentation":"Thrown by DbCommand::getConnection() (the artisan db / db:show / db:table / db:wipe console command base) when the connection name argument (or the configured default) resolves to an empty config array. The command refuses to launch the DB client against an unknown connection.","triggerScenarios":"Running `php artisan db myconn` (or db:show / db:table / db:wipe with --database=myconn) where 'database.connections.myconn' is not defined or is empty. Also fires when database.default points at an undefined connection and no argument is given.","commonSituations":"Typo in the connection name passed to --database; connection defined in a separate config file that wasn't merged; database.default misconfigured; config:cache run before the connection was added.","solutions":["Check the connection name is listed under database.connections in config/database.php.","Verify database.default is set to a defined connection when not passing an argument.","Run php artisan config:clear so a recently added connection is picked up.","List configured connections: php artisan db:show --counts or inspect config('database.connections')."],"exampleFix":"// before\nphp artisan db replica\n// where 'replica' is not in database.connections\n\n// after\n// add to config/database.php:\n'connections' => [\n    'replica' => ['driver'=>'mysql','host'=>env('DB_REPLICA_HOST'), ...],\n],\nphp artisan config:clear\nphp artisan db replica","handlingStrategy":"validation","validationCode":"$name = $request->input('connection', config('database.default'));\nif (empty(config('database.connections.'.$name))) {\n    abort(400, \"Unknown database connection [$name].\");\n}","typeGuard":"function connectionIsConfigured(string $name): bool {\n    return ! empty(config('database.connections.'.$name));\n}","tryCatchPattern":null,"preventionTips":["Whitelist the connection argument to keys present in database.connections.","Keep database.default pointed at a defined connection.","Run config:clear after adding a new connection.","Document available connection names for operators."],"tags":["console","configuration","connection"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}