Created
May 15, 2024 05:52
-
-
Save taroyanaka/c6c20e6b04bdb1deecb570d38166bbac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://tembo.io/docs/getting-started/quickstarts/nodejs | |
// https://tembo.io/docs/product/cloud/security/sslmode | |
// eyJhbGciOiJSUzI1NiIsImNhdCI6ImNsX0I3ZDRQRDIyMkFBQSIsImtpZCI6Imluc18yUDJhR2Ezb1ZkZGVISmtpeG43bXdlYXpNaHciLCJ0eXAiOiJKV1QifQ.eyJhenAiOiJodHRwczovL2Nsb3VkLnRlbWJvLmlvIiwiZXhwIjoxNzQ3Mjg1Nzc1LCJpYXQiOjE3MTU3NDk3NzUsImlzcyI6Imh0dHBzOi8vY2xlcmsudGVtYm8uaW8iLCJqdGkiOiIxMTRjZDk3ZDI0NGYxNDA5Nzc0YSIsIm5iZiI6MTcxNTc0OTc3MCwib3JnX2lkIjoib3JnXzJnVU9nTUVDTUdCR3E4RWc0R05rUVMxN1FscCIsIm9yZ19yb2xlIjoiYWRtaW4iLCJvcmdfc2x1ZyI6InRhcm8iLCJvcmdhbml6YXRpb25zIjp7Im9yZ18yZ1VPZ01FQ01HQkdxOEVnNEdOa1FTMTdRbHAiOiJhZG1pbiJ9LCJzaWQiOiJhcGktdG9rZW4iLCJzdWIiOiJ1c2VyXzJnVU9lTktSQXcyQjJKWTczNWZCeUprU1pzNCJ9.JgBIsLGFsbAfgS_KAmzUkIrnjzJ6G7ahrbcoaimEMoBAXBCR5HiL2xVnbHnK18OK-PI6aJWyqDelgxpOm-ANWCKESBE4O9JbherJhqI97p-Oxx2Hd7wlD7CZZc0wq_j5GtotuiXKiOj8uC2hM3nrjql8qj70ANKa7cVgcFHU_tOYDBGSbohXM8phuvmk0wTtP5jPG9dcYzIfaK2cdUIkHOMt3viWs9Vslkc8Ij91t69wDtGNTKk3OxO3YY8hVkO7FA3XCfT-iP25OGFjcpc4z04Cb5Dk9k7iE8xZ1xNwwfVCJRXszUJxyAeTjP1puhEAa0Ejb4Fkqs2FDWcIfrTCXA | |
// psql 'postgresql://postgres:JrcL38BkQyZoAGBu@beastly-spiritual-owlet.data-1.use1.tembo.io:5432/postgres' | |
const { Pool } = require('pg'); | |
const fs = require('fs'); | |
const connectionString = 'postgresql://postgres:JrcL38BkQyZoAGBu@beastly-spiritual-owlet.data-1.use1.tembo.io:5432/postgres'; | |
const pool = new Pool({ | |
connectionString: connectionString, | |
ssl: { | |
ca: fs.readFileSync('./ca.crt').toString(), | |
}, | |
}); | |
async function testQuery() { | |
const client = await pool.connect(); | |
try { | |
const response = await client.query('SELECT 1'); | |
console.log('foo'); | |
console.log(response.rows[0]['?column?']); | |
} finally { | |
client.release(); | |
} | |
} | |
// text table creation | |
async function createTable(){ | |
const client = await pool.connect(); | |
try { | |
const response = await client.query('CREATE TABLE IF NOT EXISTS test_table (id SERIAL PRIMARY KEY, name TEXT NOT NULL)'); | |
console.log('Table created successfully'); | |
} catch (err) { | |
console.error('Error creating table:', err); | |
} finally { | |
client.release(); | |
} | |
} | |
// insert data to text table | |
async function insertData(){ | |
const client = await pool.connect(); | |
try { | |
const response = await client.query('INSERT INTO test_table (name) VALUES ($1)', ['test']); | |
console.log('Data inserted successfully'); | |
} catch (err) { | |
console.error('Error inserting data:', err); | |
} finally { | |
client.release(); | |
} | |
} | |
// Define the SQL query to get data from the text table | |
const query = 'SELECT * FROM test_table;'; | |
// Get data from the table | |
async function getData() { | |
const client = await pool.connect(); | |
try { | |
const res = await client.query(query); | |
console.log(res.rows); // Print the retrieved data | |
} catch (err) { | |
console.error('Error retrieving data:', err); | |
} finally { | |
client.release(); | |
} | |
} | |
// Call the getData function | |
getData(); | |
// createTable(); | |
// insertData(); | |
// testQuery(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment