Skip to content

Commit

Permalink
Merge pull request #9 from Code-the-Dream-School/postgres_connection
Browse files Browse the repository at this point in the history
PostgreSQL connection setup
  • Loading branch information
erhaneth authored Jan 9, 2025
2 parents 7f70c9c + 31c498a commit 8a85591
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 3 deletions.
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);

0 comments on commit 8a85591

Please sign in to comment.