Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Koa from 'koa';
- import { Pool } from 'pg';
- import fs from 'fs';
- const app = new Koa();
- const port = process.env.PORT || 3000;
- const pool = new Pool({
- user: 'your_postgres_user',
- host: 'your_postgres_host',
- database: 'your_postgres_database',
- password: 'your_postgres_password',
- port: 5432, // Default PostgreSQL port
- });
- const createTables = async () => {
- const client = await pool.connect();
- try {
- // Read the SQL queries from the file
- const sqlQueries = fs.readFileSync('edulab.sql', 'utf8');
- // Split the SQL queries into an array
- const queries = sqlQueries.split(/;\s*$/m);
- // Execute each query
- for (const query of queries) {
- if (query.trim().length > 0) {
- await client.query(query);
- }
- }
- console.log('Tables created successfully');
- } catch (err) {
- console.error('Error creating tables:', err);
- } finally {
- client.release();
- }
- };
- app.listen(port, async () => {
- console.log(`Server running on http://localhost:${port}`);
- try {
- await createTables();
- } catch (err) {
- console.error('Error creating tables:', err);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement