Skip to content
Can't Reach Database
cyber-cute
OP

2 years 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

54 Replies

cyber-cute
OP

2 years ago

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


cyber-cute
OP

2 years ago

im working with next.js and postgressql


2 years ago

how exactly are you adding these 3 seconds?


cyber-cute
OP

2 years ago

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


2 years 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
OP

2 years 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
OP

2 years ago

npm run migrate && npm start


cyber-cute
OP

2 years ago

alsi this is my pre deploy think


cyber-cute
OP

2 years ago


2 years ago

can you show me this npm run migrate command?


2 years ago

what does it reflect to in your package.json


2 years ago

its blue, they have not yet saved the changes


2 years ago

true


2 years ago

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


2 years ago

I thought that was fixed


2 years ago

🤔


2 years ago

we dont, they likely read a very old thread


2 years ago

yep


cyber-cute
OP

2 years 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
OP

2 years ago

uihh


cyber-cute
OP

2 years ago

I think I seemed to understand


cyber-cute
OP

2 years ago

migrate should be postinstall?


2 years ago

no.


cyber-cute
OP

2 years ago

UH LOL


cyber-cute
OP

2 years ago

<:thinkies:1238577143466692739>


cyber-cute
OP

2 years ago

@Medim what the service settings tho


2 years ago

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


cyber-cute
OP

2 years ago

same :cccccc


cyber-cute
OP

2 years ago

Am I missing something?


2 years ago

link to project please



cyber-cute
OP

2 years ago

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


2 years 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
OP

2 years ago

switchyard.proxy.rlwy.net:45368


cyber-cute
OP

2 years ago

so i should use this right?


2 years ago

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


2 years ago

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


2 years ago

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


cyber-cute
OP

2 years ago

this? postgresql://postgres:[email protected]:5432/railway


cyber-cute
OP

2 years ago

oke I'll try again


cyber-cute
OP

2 years 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/node_modules/.prisma/client/runtime/library.js:124:7759)

at zn.handleAndLogRequestError (/app/node_modules/.prisma/client/runtime/library.js:124:6784)

at zn.request (/app/node_modules/.prisma/client/runtime/library.js:124:6491)

at async l (/app/node_modules/.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.


2 years 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
OP

2 years ago

uhmm


cyber-cute
OP

2 years ago

i dont understand really


cyber-cute
OP

2 years ago

oke sir thx


cyber-cute
OP

2 years ago

❤️


2 years 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
OP

2 years 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
OP

2 years ago

uhh ok I can try that


2 years ago

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


cyber-cute
OP

2 years ago

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


cyber-cute
OP

2 years ago

thx medim


2 years 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 years ago

!s


Status changed to Solved medim • over 1 year ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...