I need to retrieve data from this Postgres database
altterbr
HOBBYOP

8 months ago

My application suddenly crashed with a 502 error. I tried to follow the documentation but it didn't work. However, I was able to deploy it to another application but I need to transfer my data there.My application suddenly crashed with a 502 error. I tried to follow the documentation but it didn't work. However, I was able to deploy it to another application but I need to transfer my data there.

Solved$10 Bounty

Pinned Solution

angobello
PRO

8 months ago

What you need:
- The connection string of your source database (Which is the database you have on railway) i.e. postgresql://postgres:DOyoyYNM@shinka.proxy.rlwy.net:40345/railway
- The conenction string of your destination database (Which is the database you have on the new application you hosted on) i.e. postgresql://postgres:DOyoyYNM@shinka.proxy.rlwy.net:40345/railway

Steps:
1. Go to your railway project that has the database and create a railway function (Right Click -> Function)
2. Click on deploy so itll get setup
3. Click on the created function service and navigate to the "Variables" tab
4. On the variables tab add two variables, `DESTINATION_POSTGRES_CONNECTION_STRING` which will be the connection string of the database you have on the other application and `SOURCE_POSTGRES_CONNECTION_STRING` which will be the connection string of your database on railway
5. Make sure to click "Deploy" after you add the variables
6. Navigate to the "Source Code" tab and paste the below:
```
import { Client } from "bun:pg";

const sourceClient = new Client({

connectionString: process.env.SOURCE_POSTGRES_CONNECTION_STRING,

});

const destClient = new Client({

connectionString: process.env.DESTINATION_POSTGRES_CONNECTION_STRING,

});

async function transferAllTables() {

await sourceClient.connect();

await destClient.connect();

const tablesRes = await sourceClient.query(`

SELECT tablename

FROM pg_tables

WHERE schemaname = 'public'

`);

const tables = tablesRes.rows.map((row) => row.tablename);

for (const tableName of tables) {

const dataRes = await sourceClient.query(`SELECT * FROM "${tableName}"`);

const rows = dataRes.rows;

// Get columns

const columnNames = Object.keys(rows[0] || {});

const columnsList = columnNames.map((name) => "${name}").join(", ");

// Create table if it doesn’t exist

await destClient.query(`

CREATE TABLE IF NOT EXISTS "${tableName}" (

${columnNames.map((col) => "${col}" TEXT).join(", ")}

)

`);

for (const row of rows) {

const values = columnNames.map((col) => row[col]);

const placeholders = values.map((_, i) => $${i + 1}).join(", ");

await destClient.query(

INSERT INTO "${tableName}" (${columnsList}) VALUES (${placeholders}),

values

);

}

console.log(`white_check_mark emoji Transferred table: ${tableName}`);

}

await sourceClient.end();

await destClient.end();

console.log("rocket emoji Database transfer complete.");

}

transferAllTables().catch((err) => {

console.error("x emoji Error during transfer:", err);

});
```
7. After pasting it click on "Deploy"
8. Once it is done deploying you can check your destination postgres url to confirm it worked

NOTE: You can find a video walkthrough here: https://vimeo.com/1089293602/11227bf156?share=copy (Slight audio issues i didnt notice)

4 Replies

brody
EMPLOYEE

8 months ago

This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.

Status changed to Open brody 9 months ago


angobello
PRO

8 months ago

Which database are you using?


angobello

Which database are you using?

aniket-lodh
FREE

8 months ago

I need to retrieve data from this Postgres database


angobello
PRO

8 months ago

my bad smile emoji

To transfer your data there make sure you have a postgres db hosted on the other platform (important)

Give me a min ill send you a video on how to do it using functions


angobello
PRO

8 months ago

What you need:
- The connection string of your source database (Which is the database you have on railway) i.e. postgresql://postgres:DOyoyYNM@shinka.proxy.rlwy.net:40345/railway
- The conenction string of your destination database (Which is the database you have on the new application you hosted on) i.e. postgresql://postgres:DOyoyYNM@shinka.proxy.rlwy.net:40345/railway

Steps:
1. Go to your railway project that has the database and create a railway function (Right Click -> Function)
2. Click on deploy so itll get setup
3. Click on the created function service and navigate to the "Variables" tab
4. On the variables tab add two variables, `DESTINATION_POSTGRES_CONNECTION_STRING` which will be the connection string of the database you have on the other application and `SOURCE_POSTGRES_CONNECTION_STRING` which will be the connection string of your database on railway
5. Make sure to click "Deploy" after you add the variables
6. Navigate to the "Source Code" tab and paste the below:
```
import { Client } from "bun:pg";

const sourceClient = new Client({

connectionString: process.env.SOURCE_POSTGRES_CONNECTION_STRING,

});

const destClient = new Client({

connectionString: process.env.DESTINATION_POSTGRES_CONNECTION_STRING,

});

async function transferAllTables() {

await sourceClient.connect();

await destClient.connect();

const tablesRes = await sourceClient.query(`

SELECT tablename

FROM pg_tables

WHERE schemaname = 'public'

`);

const tables = tablesRes.rows.map((row) => row.tablename);

for (const tableName of tables) {

const dataRes = await sourceClient.query(`SELECT * FROM "${tableName}"`);

const rows = dataRes.rows;

// Get columns

const columnNames = Object.keys(rows[0] || {});

const columnsList = columnNames.map((name) => "${name}").join(", ");

// Create table if it doesn’t exist

await destClient.query(`

CREATE TABLE IF NOT EXISTS "${tableName}" (

${columnNames.map((col) => "${col}" TEXT).join(", ")}

)

`);

for (const row of rows) {

const values = columnNames.map((col) => row[col]);

const placeholders = values.map((_, i) => $${i + 1}).join(", ");

await destClient.query(

INSERT INTO "${tableName}" (${columnsList}) VALUES (${placeholders}),

values

);

}

console.log(`white_check_mark emoji Transferred table: ${tableName}`);

}

await sourceClient.end();

await destClient.end();

console.log("rocket emoji Database transfer complete.");

}

transferAllTables().catch((err) => {

console.error("x emoji Error during transfer:", err);

});
```
7. After pasting it click on "Deploy"
8. Once it is done deploying you can check your destination postgres url to confirm it worked

NOTE: You can find a video walkthrough here: https://vimeo.com/1089293602/11227bf156?share=copy (Slight audio issues i didnt notice)


Status changed to Solved chandrika 7 months ago


Loading...