A simple Node.js application demonstrating database migrations with PostgreSQL using node-pg-migrate.
- PostgreSQL database integration
- Database migrations using
node-pg-migrate - Environment-based configuration
- Simple todo management system
- Node.js (v14 or higher)
- PostgreSQL database server
- npm (Node Package Manager)
├── migrations/
│ └── 1698765432345_create-todos-table.js
├── .env.example
├── .env
├── index.js
├── package.json
└── README.md
- Clone the repository
- Install dependencies:
npm install
- Create a
.envfile based on.env.example:cp .env.example .env
- Update the
.envfile with your PostgreSQL connection details:DATABASE_URL=postgres://username:password@localhost:5432/database_name
This project uses node-pg-migrate for managing database schema changes.
- Run all pending migrations:
npm run migrate:up
- Revert the last migration:
npm run migrate:down
1698765432345_create-todos-table.js- Creates the initial
todostable with:id(Primary Key)title(varchar)completed(boolean)created_at(timestamp)
- Creates the initial
Start the application:
npm startThe application will:
- Connect to the PostgreSQL database
- Display the current timestamp
- List all todos in the database
| Variable | Description | Example |
|---|---|---|
| DATABASE_URL | PostgreSQL connection URL | postgres://username:password@localhost:5432/todos |
- Never commit the
.envfile to version control - Keep your database credentials secure
- The
.env.examplefile should not contain actual credentials
npm start- Run the applicationnpm run migrate- Run database migrationsnpm run migrate:up- Apply pending migrationsnpm run migrate:down- Revert the last migration
pg- PostgreSQL client for Node.jsnode-pg-migrate- Database migration tooldotenv- Environment variable management