Can't Reach Database

cyber-cute
FREE

3 months ago

I have read the texts in the help section, but I am quite new to railway and I don't understand anything.

Solved$10 Bounty

0 Replies

cyber-cute
FREE

3 months ago

a worker said to add 3 seconds in the forum, I added it, but the same problem still exists


cyber-cute
FREE

3 months ago

im working with next.js and postgressql


3 months ago

how exactly are you adding these 3 seconds?


cyber-cute
FREE

3 months ago

so I didn't understand and chatgpt added something 3 seconds into lib/prisma.ts (and I guess it didn't work)


3 months ago

can you undo what chatgpt suggested and show me your start command (or are you doing the migration in the pre-deploy command?)


cyber-cute
FREE

3 months ago

import { PrismaClient } from '@prisma/client';

declare global {
  // eslint-disable-next-line no-var
  var prisma: PrismaClient | undefined;
}

let prisma: PrismaClient;

const connectWithRetry = async (client: PrismaClient, retries = 5, delay = 3000) => {
  for (let i = 0; i < retries; i++) {
    try {
      await client.$connect();
      console.log(':white_check_mark: Prisma: connected');
      return;
    } catch (error) {
      console.error(`❌ error (x${i + 1}):`, error);
      if (i < retries - 1) {
        await new Promise((res) => setTimeout(res, delay));
      } else {
        throw error;
      }
    }
  }
};

// Singleton ve retry birleştirildi
if (!globalThis.prisma) {
  const client = new PrismaClient();
  await connectWithRetry(client);
  globalThis.prisma = client;
}

prisma = globalThis.prisma;

export default prisma;

cyber-cute
FREE

3 months ago

npm run migrate && npm start


cyber-cute
FREE

3 months ago

alsi this is my pre deploy think


cyber-cute
FREE

3 months ago

1369345039335035000


3 months ago

can you show me this npm run migrate command?


3 months ago

what does it reflect to in your package.json


3 months ago

its blue, they have not yet saved the changes


3 months ago

true


3 months ago

btw I thought we didn't need to wait for the private network anymore


3 months ago

I thought that was fixed


3 months ago

🤔


3 months ago

we dont, they likely read a very old thread


3 months ago

yep


cyber-cute
FREE

3 months ago

"name": "qrmenu",
"version": "0.1.1",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build --no-lint",
"start": "next start",
"postinstall": "npx prisma generate && npx prisma db push",
"lint": "next lint"
},
"prisma": {
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
},


cyber-cute
FREE

3 months ago

uihh


cyber-cute
FREE

3 months ago

I think I seemed to understand


cyber-cute
FREE

3 months ago

migrate should be postinstall?


3 months ago

no.


cyber-cute
FREE

3 months ago

UH LOL


cyber-cute
FREE

3 months ago

<:thinkies:1238577143466692739>


cyber-cute
FREE

3 months ago

@Medim what the service settings tho


3 months ago

try the public db url, I wanna see if it connects


cyber-cute
FREE

3 months ago

same :cccccc


cyber-cute
FREE

3 months ago

Am I missing something?


3 months ago

link to project please



cyber-cute
FREE

3 months ago

brody I'm not going to lie I'm used to doing everything with vercel with one click


3 months ago

you are trying to reach the database over an http domain, let's go back to the private url.

unfortunately Railway the platform cannot help with incorrect implementation.


cyber-cute
FREE

3 months ago

switchyard.proxy.rlwy.net:45368


cyber-cute
FREE

3 months ago

so i should use this right?


3 months ago

i'm wondering if it's because of his app being CSR


3 months ago

that is the public TCP proxy, please go back to using the private url for the database connection


3 months ago

yeah but CSR apps still won't talk to the database from the user's browser


cyber-cute
FREE

3 months ago

this? postgresql://postgres:x@postgres.railway.internal:5432/railway


cyber-cute
FREE

3 months ago

oke I'll try again


cyber-cute
FREE

3 months ago

Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
PrismaClientInitializationError:
Invalid prisma.appSettings.findUnique() invocation:
Can't reach database server at postgres.railway.internal:5432
Please make sure your database server is running at postgres.railway.internal:5432.
at zn.handleRequestError (/app/nodemodules/.prisma/client/runtime/library.js:124:7759) at zn.handleAndLogRequestError (/app/nodemodules/.prisma/client/runtime/library.js:124:6784)
at zn.request (/app/nodemodules/.prisma/client/runtime/library.js:124:6491) at async l (/app/nodemodules/.prisma/client/runtime/library.js:133:9778)
at async l (/app/.next/server/app/page.js:1:1253)
Export encountered an error on /page: /, exiting the build.


3 months ago

the private network is not available during build, you will need to switch to SSR during runtime instead of SSG during build, or temporarily switch to the public network just for build time.

I'll let the community help with the implementation details.


cyber-cute
FREE

3 months ago

uhmm


cyber-cute
FREE

3 months ago

i dont understand really


cyber-cute
FREE

3 months ago

oke sir thx


cyber-cute
FREE

3 months ago

❤️


3 months ago

You got two approaches to this issue:

  • Switching to SSR (easier one but depends on your nextjs project)

  • Conditionally switch between private (on runtime) and public (on build) db url, you can identify in which step nextjs is with the NEXT_PHASE env var


cyber-cute
FREE

3 months ago

frankly, the project is not mine, I was just helping someone or he will try to host the next.js project from cpanel


cyber-cute
FREE

3 months ago

uhh ok I can try that


3 months ago

The easier approach is to switch to SSR, that's a one liner depending on the project


cyber-cute
FREE

3 months ago

I'm really confused lol I need a drink of water.


cyber-cute
FREE

3 months ago

thx medim


3 months ago

No worries, sorry I couldn't be more helpful! this is more of a code issue and I have some skill issues with nextjs <:kekw:788259314607325204>


2 months ago

!s


Status changed to Solved medim 2 months ago