serverless-pg icon indicating copy to clipboard operation
serverless-pg copied to clipboard

serverless-pg with Netlify

Open arnaudjnn opened this issue 2 years ago • 6 comments

I'm using Netlify functions for serverless

When I tried serverless-pg I had the following error:

[ERROR] Could not resolve "pg-native"
    node_modules/pg/lib/native/client.js:4:21:
      4 │ var Native = require('pg-native')

So I installed pg-native, working well locally but then I have the following error with deploy: https://answers.netlify.com/t/unable-to-publish-function-using-postgres/12293

Do you have an install with Netlify? How can I fix the error with libpq?

arnaudjnn avatar Jun 09 '22 10:06 arnaudjnn

Any chance you have this variable set? NODE_PG_FORCE_NATIVE https://github.com/brianc/node-postgres/blob/28ac2a17bce287cfa458153dcabe3ca06ca0e28f/packages/pg/lib/index.js#L28

I have never used Netlify myself, but I have found something: https://github.com/brianc/node-postgres/issues/1894#issuecomment-543010595

MatteoGioioso avatar Jun 09 '22 10:06 MatteoGioioso

@MatteoGioioso I tried to set NODE_PG_FORCE_NATIVE but this does not solve the error.

Is it possible to use postgres instead pg as detailed here?

https://answers.netlify.com/t/continues-deployment-of-functions-with-postgresql/44730/30

arnaudjnn avatar Jun 10 '22 06:06 arnaudjnn

@arnaudjnn I think you should unset it.

regarding to use another library, I think it should be possible: https://github.com/MatteoGioioso/serverless-pg/blob/37f52a95139f7581b87c69797dec61899b6891dd/tests/index.test.js#L565

however the API must be compatible

MatteoGioioso avatar Jun 10 '22 08:06 MatteoGioioso

Sorry no I haven't this variable NODE_PG_FORCE_NATIVE

How would you do to use another library like postgres ?

Would be awesome to get an example working with Netlify!

arnaudjnn avatar Jun 10 '22 09:06 arnaudjnn

You could try something like that

const yourDriver = require("custom-driver")

const client = new ServerlessClient({
  ...dbConfig,
  debug: true,
  library: yourDriver
});

although I see that the library you are suggesting use quite different API. I will test something and let you know.

PS: have you tried this? https://github.com/brianc/node-postgres/issues/1894#issuecomment-543030502

MatteoGioioso avatar Jun 10 '22 11:06 MatteoGioioso

It may be worth considering running the library through a bundler to make this problem go away.

I've spent most of today trying to get pg through parcel without any luck, and it would certainly make some sense for this library to deliberately excise pg-native for ease of bundling.

AWare avatar Dec 20 '22 17:12 AWare