Step-by-Step Guide: Setting up Node.
js with
PostgreSQL Online
This guide will walk you through setting up a Node.js backend project in VS Code and linking it to a
PostgreSQL database hosted online (ElephantSQL).
1 Open VS Code and create a new folder for your project.
2 Open the integrated terminal in VS Code (``Ctrl + ` ``).
3 Run ``npm init -y`` to create a ``package.json`` file.
4 Install Express and PostgreSQL libraries: ``npm install express pg``.
5 Install Nodemon for auto-restart during development: ``npm install --save-dev nodemon``.
6 Create a new file ``server.js`` (or ``index.js``) for your main server code.
7 Go to ElephantSQL.com, create an account, and set up a new PostgreSQL instance.
8 Copy the database URL from ElephantSQL — you will need it to connect from Node.js.
9 In your project, create a file ``.env`` and store your database URL like:
``DATABASE_URL=your_url_here``.
10 Install dotenv in Node.js to load environment variables: ``npm install dotenv``.
11 Write your server code to connect to PostgreSQL using the ``pg`` library.
12 Test your setup by running ``nodemon server.js`` and ensuring the database connection works.
Recommended VS Code Extensions:
• ES7+ React/Redux/React-Native snippets (for JavaScript shortcuts)
• Prettier - Code formatter
• REST Client (to test APIs without leaving VS Code)
• DotENV (syntax highlighting for .env files)
• vscode-database (optional for database browsing)
Suggested Project Structure:
• project-folder/
• ■■■ node_modules/
• ■■■ server.js
• ■■■ package.json
• ■■■ .env
• ■■■ README.md
You are now ready to build and run your Node.js backend connected to PostgreSQL online!