Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgreSQL connection setup #9

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 244 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.4.7",
"express": "^4.18.2",
"express-favicon": "^2.0.4",
"mongoose": "^7.0.1",
"morgan": "^1.10.0"
"morgan": "^1.10.0",
"pg": "^8.13.1"
},
"devDependencies": {
"nodemon": "^2.0.21"
Expand Down
20 changes: 20 additions & 0 deletions src/db/connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require('dotenv').config(); // Load environment variables from .env
const {Client} = require('pg');

// PostgreSQL connection using the connection string
const client = new Client({
connectionString: process.env.DATABASE_URL, // Load connection string from .env
});


// Connect to PostgreSQL
const connectDB = () => {
client.connect().then(() => {
console.log('Connected to PostgreSQL database successfully!');
})
.catch(err => {
console.error('Error connecting to PostgreSQL database:', err.stack);
});
}

module.exports = connectDB
6 changes: 5 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const { PORT = 8000 } = process.env;
const { PORT = 8001 } = process.env;
const app = require("./app");

const connectDB = require("./db/connect");

connectDB(); // This will execute the connection string to connect to the database.

const listener = () => console.log(`Listening on Port ${PORT}!`);
app.listen(PORT, listener);
Loading