Repository navigation
Description
Discussed in #6963
Originally posted by pfried May 6, 2021
This is a question on how to setup timescale with prisma. Thanks to the post from @janpio I tried to create a similar setup. So this is
related to ##3228
My goal is to ingest IoT data into timescaledb with the help of prisma since the whole raw sql stuff is just bad (I know some stuff will stay raw sql stuff for now, especially timescaledb functionality).
My desired setup
- Timescale 2.2.0 running on Timescale Forge
- Prisma / Prisma Client 2.22.0 used by AWS Lambda (will do a lambda layer / package as suggested by the docs).
Following the steps
The first step would be to create a migration which will install the timescale extension since the shadow database will run the migrations too (it is obiously already installed on Laravel forge). So the migration.sql file will look like:
CREATE EXTENSION IF NOT EXISTS timescaledb VERSION "2.2.0";
Case 1: Forge Database without shadow database
Try to run the npx prisma migrate dev command on the timescale forge database will result in an error since the command tries to create a table:
Error: Database error
Error querying the database: db error: ERROR: tsdb_admin: database prisma_migrate_shadow_db_ab8fe7fa-8d81-4493-bafe-bf5645e6d2cc is not an allowed database name
HINT: Contact your administrator to configure the "tsdb_admin.allowed_databases"
Following the suggestions in #4571 and here: https://www.prisma.io/docs/concepts/components/prisma-migrate#shadow-database the guess would be that it is not possible to use the timescale forge service for the shadow db. Which leads to:
Case 2: Forge database with shadow database on localhost
My localhost is a postgres 13 instance on windows with the timescale extension available.
Adding a shadow database in the environment and running the npx prisma migrate dev will result in an error telling that the timescaledb extension is already loaded (both instances run 2.2.0):
Error: P3006
Migration `20210505152706_` failed to apply cleanly to the shadow database.
Error:
Database error
Error querying the database: db error: FEHLER: extension "timescaledb" has already been loaded with another version
DETAIL: The loaded version is "2.2.0".
If the timescale forge database has already has data a reset is suggested and if executed will result in:
Error: P3016
The fallback method for database resets failed, meaning Migrate could not clean up the database entirely. Original error:
Database error
Error querying the database: db error: ERROR: must be owner of view pg_stat_statements
0: sql_migration_connector::best_effort_reset
Case 3 Local Timescale database
If no shadow database is explicitly given (meaning the migrate command will create one) everything works on the local machine (different to Case 1).
Case 4 Local Timescale database with explicit shadow database on the same server
If I explicitly set a shadow database on my local machine I will end up in the same error as Case 2:
Error: P3006
Migration `20210505152706_` failed to apply cleanly to the shadow database.
Error:
Database error
Error querying the database: db error: FEHLER: extension "timescaledb" has already been loaded with another version
DETAIL: The loaded version is "2.2.0".
Activity
pantharshit00 commentedon Jul 28, 2021
Hmm, we should close this in favour of #3228 until we decide to properly support timescale.
pfried commentedon Jan 20, 2022
Okay, I gave this a new try ([email protected]):
Followed along the original gist and the blogpost from @orpheousff8
The first step is to load the extension by adding to the migration (first line)
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;Which results in (on fresh deployed DBs):
I do not get why this happens since I can run this statement in both databases without an error and also the
IF NOT EXISTSis added.Maybe the prisma clients runs statements before running the migration?
pfried commentedon Jan 20, 2022
The docs say something like:
But this does not work for e.g. Timescale Cloud where the extension is pre installed
PS: I also tried to do:
But that does not work either
orpheousff8 commentedon Jan 20, 2022
@pfried I've tried my tutorial only on TimescaleDB docker image, never tried it on Timescale cloud.
By the way, I once saw the error:
db error: ERROR: extension "timescaledb" has already been loaded with another versionwhen I tried to do (not first) migration with thisCREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;SQL command on the DB that has already been installed the extension at the first migration.(Even you use TimescaleDB docker image, you always have to install the extension as per my tutorial at the first migration anyway. I don't know why...)
In your case, did you tried not to do anything with the EXTENSION commands, and just skip to next steps? Not sure if it works...
pfried commentedon Jan 20, 2022
@orpheousff8 You cannot access timescale functions inside the session context if you do not call that function, at least I get errors when I call them without the statement
Addtional info tough: The migrations will run but error in the console. The whole thing is not rolled back, so it actually works. To me this looks more like the migrate command translates the warning into an error
pfried commentedon Jan 20, 2022
Funnily enough if you run
prisma migrate resettwice quickly after each other (maybe connection reuse?) it works. Sry for not really having an idea what the root cause of all of this is, but I can only share observations8 remaining items
janpio commentedon Jul 14, 2023
Happy to create the Timescaledb cloud databases myself if you just tell me roughly what to click and then how to use it in the app from your repository.
pfried commentedon Jul 17, 2023
Sure there is a free trial you can use at: https://console.cloud.timescale.com/signup
I created an MRE at https://github.com/Nantis-GmbH/prisma-timescale
janpio commentedon Jul 17, 2023
Before I get to the extension and reset problem, I confirmed along the way that the shadow database problem (that it can not be created automatically on Timescale Cloud) is handled properly today: #20266 (comment)
janpio commentedon Jul 17, 2023
Ok, on to the
migrate devprboelm:I can definitely reproduce the problem that a migration file that installs the
timescaledbextension does not work with a Timescale Cloud database as the shadow database:As you described, I can run
CREATE EXTENSION IF NOT EXISTS "timescaledb";on a clean (!) TimescaleDB database to reinstall the extension though, why does this not work here?It seems that as a first step,
migrate devcleans up the provided explicit shadow database:It does this by running these commands:
Afterward,
publicis in a clean state, without thetimescaledbextension installed.Now the weird bit: Now I can not run
CREATE EXTENSION ...any more on this database but get exactly the error message you are describing!So these command let you reproduce the situation:
If I now disconnect from the database server (as indicated in the error message), and then reconnect I can run
CREATE EXTENSION ...again and it will be correctly installed.Is this a PostgreSQL quirk of installing extensions?
Or a Timescale extension problem in some form?
I think the
migrate resetproblem, that only every second run works, that @rickbatka also reported, is essentially the same thing: Ifresethas to actually reset the schema, Migrate will not be able to execute theCREATE EXTENSIONstatement. Then on the next try, it does not because the schema is already fully clear so it can just run the migrations - andCREATE EXTENSIONwill work.Super weird, but it makes "sense" from that perspective.
[-]Timescale Setup issues[/-][+]Timescale does not work with `migrate dev` and `migrate reset`: `extension "timescaledb" has already been loaded with another version`[/+]janpio commentedon Jul 17, 2023
I opened an issue with Timescale asking for information: timescale/timescaledb#5884
RyKilleen commentedon Feb 28, 2024
Is there a chance anyone here has found a reasonable workaround since July 2023? As a recent convert to Timescale and an advocate of Prisma, I find myself in this situation.
alexanderdavide commentedon Sep 3, 2024
For my local development purposes, I decided to use the following workaround which is just a bit nicer than re-executing manually and explaining every new developer what is going on.
pdesjardins90 commentedon Mar 28, 2025
For anyone still hitting their heads with that here you go:
Instead of
npm exec -- prisma migrate reset --force --skip-seeddo