|
| 1 | +# Postgres Demo |
| 2 | + |
| 3 | +A Spring Boot application demonstrating PostgreSQL database connectivity with Flyway migrations, built with Gradle (Kotlin DSL). |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Spring Boot 3.2.1 application |
| 8 | +- PostgreSQL database connection using Spring Data JDBC |
| 9 | +- Flyway database migrations (managed by Spring Boot) |
| 10 | +- Configuration via `application.yml` with Spring's built-in environment variable support |
| 11 | +- Environment variable support via `.env` files (using spring-dotenv) |
| 12 | +- Docker Compose setup for local PostgreSQL instance |
| 13 | +- Gradle Kotlin DSL build configuration |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- Java 17 or higher |
| 18 | +- Docker and Docker Compose (for running PostgreSQL) |
| 19 | +- Gradle (wrapper included) |
| 20 | + |
| 21 | +## Project Structure |
| 22 | + |
| 23 | +``` |
| 24 | +postgres_demo/ |
| 25 | +├── app/ |
| 26 | +│ ├── build.gradle.kts # Gradle build configuration |
| 27 | +│ └── src/ |
| 28 | +│ └── main/ |
| 29 | +│ ├── java/ |
| 30 | +│ │ └── com/codesociety/ |
| 31 | +│ │ ├── PostgresDemoApplication.java # Spring Boot main class |
| 32 | +│ │ └── DemoRunner.java # CommandLineRunner for demo |
| 33 | +│ └── resources/ |
| 34 | +│ ├── application.yml # Spring Boot configuration |
| 35 | +│ └── db/migration/ |
| 36 | +│ ├── V1__create_users_table.sql # Flyway migration |
| 37 | +│ └── V2__create_posts_table.sql # Flyway migration |
| 38 | +├── docker-compose.yml # PostgreSQL Docker setup |
| 39 | +├── .env # Environment variables (local) |
| 40 | +└── .env.example # Environment variables template |
| 41 | +``` |
| 42 | + |
| 43 | +## Setup |
| 44 | + |
| 45 | +1. **Copy environment variables:** |
| 46 | + ```bash |
| 47 | + cp .env.example .env |
| 48 | + ``` |
| 49 | + |
| 50 | +2. **Start PostgreSQL with Docker Compose:** |
| 51 | + ```bash |
| 52 | + docker-compose up -d |
| 53 | + ``` |
| 54 | + |
| 55 | +3. **Verify PostgreSQL is running:** |
| 56 | + ```bash |
| 57 | + docker-compose ps |
| 58 | + ``` |
| 59 | + |
| 60 | +## Configuration |
| 61 | + |
| 62 | +Spring Boot automatically loads configuration from multiple sources in this order: |
| 63 | + |
| 64 | +### 1. application.yml (Spring Boot standard) |
| 65 | +Located at `app/src/main/resources/application.yml`: |
| 66 | +```yaml |
| 67 | +spring: |
| 68 | + datasource: |
| 69 | + url: ${DB_URL:jdbc:postgresql://localhost:5432/demo_db} |
| 70 | + username: ${DB_USER:demo_user} |
| 71 | + password: ${DB_PASSWORD:demo_password} |
| 72 | +``` |
| 73 | +
|
| 74 | +Spring Boot supports `${ENV_VAR:default_value}` syntax natively! |
| 75 | + |
| 76 | +### 2. .env file (via spring-dotenv) |
| 77 | +Create a `.env` file in the project root: |
| 78 | +``` |
| 79 | +DB_URL=jdbc:postgresql://localhost:5432/demo_db |
| 80 | +DB_USER=demo_user |
| 81 | +DB_PASSWORD=demo_password |
| 82 | +``` |
| 83 | + |
| 84 | +### 3. Environment variables |
| 85 | +Export environment variables directly: |
| 86 | +```bash |
| 87 | +export DB_URL=jdbc:postgresql://localhost:5432/demo_db |
| 88 | +export DB_USER=demo_user |
| 89 | +export DB_PASSWORD=demo_password |
| 90 | +``` |
| 91 | + |
| 92 | +## Running the Application |
| 93 | + |
| 94 | +### Quick start (includes Docker setup): |
| 95 | +```bash |
| 96 | +./start.sh |
| 97 | +``` |
| 98 | + |
| 99 | +### Run with Gradle: |
| 100 | +```bash |
| 101 | +./gradlew :app:bootRun |
| 102 | +``` |
| 103 | + |
| 104 | +### Build the application: |
| 105 | +```bash |
| 106 | +./gradlew :app:build |
| 107 | +``` |
| 108 | + |
| 109 | +### Run as a JAR: |
| 110 | +```bash |
| 111 | +./gradlew :app:bootJar |
| 112 | +java -jar app/build/libs/app.jar |
| 113 | +``` |
| 114 | + |
| 115 | +## What the Application Does |
| 116 | + |
| 117 | +1. Loads environment variables from `.env` file (via spring-dotenv) |
| 118 | +2. Reads configuration from `application.yml` (Spring Boot handles this) |
| 119 | +3. Runs Flyway database migrations automatically (Spring Boot integration) |
| 120 | +4. Connects to PostgreSQL database using Spring's DataSource |
| 121 | +5. Queries and displays users and posts from the database using JdbcTemplate |
| 122 | + |
| 123 | +## Database Schema |
| 124 | + |
| 125 | +The application creates two tables: |
| 126 | + |
| 127 | +**Users table:** |
| 128 | +```sql |
| 129 | +CREATE TABLE users ( |
| 130 | + id SERIAL PRIMARY KEY, |
| 131 | + username VARCHAR(100) NOT NULL UNIQUE, |
| 132 | + email VARCHAR(255) NOT NULL UNIQUE, |
| 133 | + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 134 | + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP |
| 135 | +); |
| 136 | +``` |
| 137 | + |
| 138 | +**Posts table:** |
| 139 | +```sql |
| 140 | +CREATE TABLE posts ( |
| 141 | + id SERIAL PRIMARY KEY, |
| 142 | + user_id INTEGER NOT NULL, |
| 143 | + title VARCHAR(255) NOT NULL, |
| 144 | + content TEXT, |
| 145 | + published BOOLEAN NOT NULL DEFAULT false, |
| 146 | + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 147 | + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 148 | + CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) |
| 149 | +); |
| 150 | +``` |
| 151 | + |
| 152 | +## Docker Commands |
| 153 | + |
| 154 | +**Start PostgreSQL:** |
| 155 | +```bash |
| 156 | +docker-compose up -d |
| 157 | +``` |
| 158 | + |
| 159 | +**Stop PostgreSQL:** |
| 160 | +```bash |
| 161 | +docker-compose down |
| 162 | +``` |
| 163 | + |
| 164 | +**View PostgreSQL logs:** |
| 165 | +```bash |
| 166 | +docker-compose logs -f postgres |
| 167 | +``` |
| 168 | + |
| 169 | +**Connect to PostgreSQL CLI:** |
| 170 | +```bash |
| 171 | +docker-compose exec postgres psql -U demo_user -d demo_db |
| 172 | +``` |
| 173 | + |
| 174 | +**Stop and remove volumes (delete data):** |
| 175 | +```bash |
| 176 | +docker-compose down -v |
| 177 | +``` |
| 178 | + |
| 179 | +## Spring Boot Features Used |
| 180 | + |
| 181 | +- **Auto-configuration:** Spring Boot automatically configures DataSource, JdbcTemplate, and Flyway |
| 182 | +- **Environment variable support:** Built-in `${VAR:default}` syntax in application.yml |
| 183 | +- **Flyway integration:** Migrations run automatically on startup |
| 184 | +- **CommandLineRunner:** DemoRunner executes after the application starts |
| 185 | + |
| 186 | +## Dependencies |
| 187 | + |
| 188 | +- Spring Boot Starter (3.2.1) |
| 189 | +- Spring Boot Starter Data JDBC |
| 190 | +- PostgreSQL JDBC Driver (42.7.1) |
| 191 | +- Flyway Core (managed by Spring Boot) |
| 192 | +- Flyway PostgreSQL |
| 193 | +- Spring Dotenv (4.0.0) - for .env file support |
| 194 | + |
| 195 | +## Troubleshooting |
| 196 | + |
| 197 | +**Connection refused:** |
| 198 | +- Ensure PostgreSQL is running: `docker-compose ps` |
| 199 | +- Check the database URL in `.env` matches your setup |
| 200 | + |
| 201 | +**Flyway migration errors:** |
| 202 | +- Reset the database: `docker-compose down -v && docker-compose up -d` |
| 203 | +- Check migration files in `app/src/main/resources/db/migration/` |
| 204 | + |
| 205 | +**Build errors:** |
| 206 | +- Clean and rebuild: `./gradlew clean :app:build` |
| 207 | +- Ensure Java 17+ is installed: `java -version` |
| 208 | + |
| 209 | +**Tests fail:** |
| 210 | +- The tests require a running database or use H2 in-memory database |
| 211 | +- To skip tests: `./gradlew :app:build -x test` |
| 212 | + |
0 commit comments