AdonisJS Lucid: Connecting to CockroachDB

Sam Ngu
1 min readFeb 9, 2024

CockroachDB is great. But it was not officially supported by Lucid. So I was thinking of using the postgres driver, and it should just work, right?

Wrong! Knex throws this error right in my face:

Cannot read properties of null (reading ‘1’)

at Client_PG._parseVersion (/node_modules/knex/lib/dialects/postgres/index.js:135:56)

After digging around on the internet, it turns out that Knex could not parse the version information from Cockroach DB behind the scenes due to naming issues. See the GitHub issue below for more details.

Solution

To workaround this issue, we can manually pass in the database version in our database configuration file.

// config/database.ts

const dbConfig = defineConfig({
connection: 'postgres',
connections: {
postgres: {
client: 'pg',
// Pass in your cockroach db version
version: '23.2.0',
connection: {
// for eg postgresql://root@localhost:26257/defaultdb?sslmode=disable
connectionString: env.get("DB_CONNECTION"),
},
migrations: {
naturalSort: true,
paths: ['database/migrations'],
},
},
});

Once this is done, you should be good to go! Hopefully this short tutorial helped you.

--

--