Prisma DB Can't connect to AWS RDS - postgresql

I have a nextjs project that's using prismaDB for the ORM. I'm able to connect just fine to my local postgres db but I'm getting this error when running npx prisma migrate.
Error: P1001: Can't reach database server at db-name.*.us-west-2.rds.amazonaws.com:5432.
schema.prisma:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
//url = "postgresql://master_username:master_password#aws_host:5432/db_name"
}
The RDS db is currently public and I'm positive that I've copied over the RDS credentials correctly. There doesn't seem to be anything I should be including for the connection to work but I'm not getting any other info as to why I can't reach the db server.

Seems like you have to replace db-name.*.us-west-2.rds.amazonaws.com with the name of your actual database, unless you replaced it for the purpose of asking this question. Specifically the part where it says db-name.*.
Docs: https://www.prisma.io/docs/reference/api-reference/error-reference#common
P1001 indicates that it couldn't find the database given the connection string, NOT necessarily that the credentials you provided were wrong. Make sure you're specifying the correct database name/host and whatever else you need to make it work for AWS.

Somehow I was able to connect to RDS after deleting and creating a new DB for the third time. I confirmed connection through pgAdmin then tried it again my app deployed to vercel.

Related

Connecting to postgres on supabase via pgBouncer from prisma

Problem
running out of database connections in prod leading to errors like this;
Error:
Invalid `prisma.queryRaw()` invocation:
Timed out fetching a new connection from the connection pool. More info: http://pris.ly/d/connection-pool (Current connection pool timeout: 10, connection limit: 5)
at Object.request (/usr/src/app/node_modules/#prisma/client/runtime/index.js:45629:15)
at async Proxy._request (/usr/src/app/node_modules/#prisma/client/runtime/index.js:46456:18)
Situation
multiple API containers running in Google Cloud Run running node/express/prisma API
using Supabase's hosted postgres. In supabase Settings > Database, Connection Pooling is enabled.
in db connection string, using :6543/postgres?pgbouncer=true
Attempt to diagnose
in supabase, Database > Roles, I can see a list of roles and the number of connections for each. pgBouncer has 0 and the role which my application uses has several.
If I query pg_stat_activity, I can see connections for the usename which is used by my application, and client_addr values representing ip addresses for a couple of different container instances. Are these "forwarded on" from pgBouncer? or have they bypassed pgBouncer entirely?
I am not familiar with what either of these should look like if I were using pgBouncer correctly so it's hard for me to tell what's going on.
I assume this means that I either haven't configured pgBouncer correctly, or I'm not connecting to it properly, or both. I'd be really grateful if someone could point out how I could either check or fix my connection to pgBouncer and clarify what I should see in pg_stat_activity if I was correctly connected to pgBouncer. Thanks.
Figured out what's going wrong here, so writing out how I fixed it in case anyone else runs into this issue.
Better understanding of the problem
in my prisma schema file I'm getting my database url from the env
datasource db {
provider = "postgresql"
url = env("SUPABASE_POSTGRES_URL")
}
and when I'm instantiating the prisma client I'm using the same variable
export const prisma = new PrismaClient({
datasources: {
db: {
url: process.env.SUPABASE_POSTGRES_URL,
},
},
});
I have a build trigger in Google Cloud Build that builds containers when branches that are merged into certain other branches, eg when PRs are merged in to master, build new containers and deploy them to prod.
In the build trigger, the SUPABASE_POSTGRES_URL value is set in the env, using :5432 which connects directly to postgres, bypassing pgBouncer. This is a requirement for prisma migrations which can't be run through pgBouncer.
The Google Cloud Run container env vars specify a different value for SUPABASE_POSTGRES_URL however it looks like the this not being used, and instead the direct-to-postgres :5432 value is used while the app is running, to connect to the db and run queries - so pgBouncer was permanently bypassed.
Solution
where the prisma client is instantiated, I'm using a second env var. It turns out that prisma uses the env var referenced in the schema file for the DB URL for migrations and the db url in the client instantiation for queries when the app is running, and you can happily have two completely separate values for these two URLs.
export const prisma = new PrismaClient({
datasources: {
db: {
url: process.env.SUPABASE_PGBOUNCER_URL,
},
},
});
Now, SUPABASE_POSTGRES_URL is still populated from the build trigger, but it doesn't get used at runtime; instead I set SUPABASE_PGBOUNCER_URL in the Google Cloud Run env vars and that gets used during the prisma client instantiation, so queries a run through pgBouncer.
Result
Effective Prisma migrations direct to postgres
Effective connection pooling by running queries through pgBouncer

Prisma with Bit.io connection issue

I have a next.js app setup with prisma (v3.13) as the ORM. I am testing out bit.io for db hosting, and I am getting this error when trying to connect with the client. Everything works as intended when I use a local postgres db. I'm currently using a connection string that looks like the following:
DATABASE_URL="postgresql://[username]:[password]#db.bit.io/[username]/[dbname]"
I am trying to run prisma db push and getting the following error
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "eli-front/rankstl", schema "public" at "db.bit.io:5432"
Error: P1000: Authentication failed against database server at `db.bit.io`, the provided database credentials for `(not available)` are not valid.
Please make sure to provide valid database credentials for the database server at `db.bit.io`.
I am assuming the core of the issue has to due with the part of the error that says credentials for '(not available)' as if something isn't loading correctly.
Using the failing connection string with psql works completely fine, but not with prisma.
There are two things that need to be done in order for bit.io to work with Prisma.
Database names must be formatted as username.dbname rather than username/dbname. bit.io supports a number of different separator characters in the database name because different clients have different requirements around permissible characters in database names.
You have to create a second database on bit.io to use as a "shadow database." By default, this is done automatically—a shadow database is created, used, and deleted. However, most cloud database providers don't allow use of the CREATE DATABASE, so a shadow database must be created explicitly. See the prisma docs for details.
See the bit.io docs on connecting with Prisma for more details on setting up a minimum working connection.

Not able to make postgres database call on heroku using Play Framework Application

I am build REST api using play framework 2.8 , I am able to start the application and call some service. Facing issue when trying to connecting with postgres database. Logs are below -
Error opening connection for database: {}org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host "3.87.112.32", user "abc", database "xyz", SSL off
Tried looking solution for this and foudn have to add sslmode=require in query param while connection to database.
When log the url sslmode is already mentioned.
jdbc:postgresql://abc.amazonaws.com:5432/xyz?password=1234&sslmode=require&user=xyz
I am reading this property from heroku env variable JDBC_DATABASE_URL for obtaining database connection.
This issue can be fixed by adding addition query parameters apart from sslmode=require which are ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory after adding this the new URL become jdbc:postgresql://abc.amazonaws.com:5432/xyz?password=1234&sslmode=require&user=xyzssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

Connect to Azure Database for PostgreSQL server with Prisma

I am looking for a way to connect my Next.js application with Azure Database for PostgreSQL server. Prisma seems to work well with Next.js but I can't figure connection string or if it is even supported database.
Example:
DATABASE_URL = 'postgresql://johndoe:randompassword#localhost:5432/mydb?schema=public'
The host I am trying to connect to:
posgreservertest.postgres.database.azure.com:5432
So it should be something like "...#posgreservertest.postgres.database.azure.com:5432/mydb?".
I am trying to npx prisma introspect and this is how my cmd looks like:
PS C:\Web stuff\nextjs\test> npx prisma introspect
Environment variables loaded from prisma\.env
Prisma schema loaded from prisma\schema.prisma
Datasource "db": PostgreSQL database "postgres", schema "public" at "posgreservertest.postgres.database.azure.com:5432"
Introspecting based on datasource defined in prisma\schema.prisma …
Error: P1001
Can't reach database server at posgreservertest.postgres.database.azure.com:`5432`
Please make sure your database server is running at posgreservertest.postgres.database.azure.com:`5432`.
Is it even possible to connect to Connect to Azure Database for PostgreSQL server with Prisma as it doesn't feature this option in their supported databases?
Make sure you've set your database as publicly available.
Add a firewall rule to allow connections from your IP. If you aren't concerned much about security you can allow connections from any IP. Just set 0.0.0.0-255.255.255.255 rule.
Ensure that you are using the full username id. For Azure PostgreSQL server id consists of two parts <username>#<servername>. In your following example full connection string will look like this
postgresql://username#servername:password#servername.postgres.database.azure.com:5432/dbname?your=options
If you've set SSL enforcing on your server make sure to add sslmode=require option

Connecting to MongoDB from AWS Glue

I am trying to create a connection from AWS Glue to MongoDB, but when I test the connection it fails with error: "Check that your connection definition references your Mongo database with correct URL syntax, username, and password. Exiting with error code 30". I know that my connection parameters are correct because I can connect with the same host, port, database, user name, and password from another client application (DataGrip). And I know that my VPC configuration should be correct too because I have another connection in the Glue, to connect to the PostgreSQL database on-premise with public IP, that works just fine.
My MongoDB version is 4.4.1. I am out of ideas what else can cause the problem. Is anyone successfully connects to MongoDB form Glue and run the Crawler?