What is a database hibernation ?
Scheduler with SQLite only
I am having this idea in my head and i tried, but cannot get it to work so far. My goal is to have a Laravel SAAS App running in Cloud with Task Scheduler Enabled and Postgres Hibernation.
So lets explain: I have multiple SaaS Services which use Postgre Database i would love to migrate them to Cloud. They fetch some Data like once and hours or every six hours from somewhere and 99% of the rest of the time they are idle. So when i push them to cloud end enable the scheduler they would never hibernate the database.
So my idea:
I know that sqlite is officially not supported on cloud, but hear me out.
What if i can setup my application to use my postgres database and only my scheduler / queue db to sqllite. I tried that, and it works, but for some reason the postgres still does not hibernate.
So what i tried.
- Set Postgres to Hibernate after 30 seconds.
- No Hibernation on the App Worker
Use normal database connection:
DB_CONNECTION=pgsql
Add something like this to our AppSericeProvider:
public function boot(): void
{
SCAlert::observe(SCAlertObserver::class);
$dbPath = database_path('database.sqlite');
if (!file_exists($dbPath)) {
file_put_contents($dbPath, '');
$migratoinPath = "2025_05_27_094735_jobs_to_sqlite";
DB::table('migrations')->where('migration', $migratoinPath)->delete();
Artisan::call('migrate', [
'--force' => true,
'--path' => 'database/migrations/' . $migratoinPath . '.php',
]);
}
}
dont forget to update the migration:
Schema::connection('sqlite')->create('jobs', function (Blu...
and update your queue.php:
'database' => [
'driver' => 'database',
'connection' => 'sqllite',
'table' => env('DB_QUEUE_TABLE', 'jobs'),
'queue' => env('DB_QUEUE', 'default'),
'retry_after' => (int) env('DB_QUEUE_RETRY_AFTER', 90),
'after_commit' => false,
],
So this works and the scheduler logic is gone to sqllite instead of the pgsql and everything so far is working fine and perfectly but the pgsql is not hibernating:
I have this test task:
Schedule::call(function () {
Log::info('hourly schedule started');
})
->name('schedule-hourly')
->hourly();
My assumtion was, that this simple task would not need the pgsql database and just use the sqllite database for scheduling. So i assume for some reason, i not know, the pgsql connection is still spun up when the scheduler is triggered, but i have no idea on how i figure this out.
I konw that this have downsides, but it would cut my costs by alot, because my database are not going to sleep at any time for know.
Please or to participate in this conversation.