|
| 1 | +# PostgreSQL Docker Setup |
| 2 | + |
| 3 | +This directory contains scripts to easily start and stop a PostgreSQL database using Docker. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- [Docker](https://www.docker.com/products/docker-desktop/) |
| 8 | +- [Docker Compose](https://docs.docker.com/compose/install/) (included in Docker Desktop) |
| 9 | + |
| 10 | +## Default Configuration |
| 11 | + |
| 12 | +- **PostgreSQL Version**: 16 (latest stable) |
| 13 | +- **Port**: 5432 |
| 14 | +- **Username**: postgres |
| 15 | +- **Password**: postgres |
| 16 | +- **Database**: mydb |
| 17 | +- **Data Volume**: postgres_data (persisted between restarts) |
| 18 | + |
| 19 | +## Getting Started |
| 20 | + |
| 21 | +Make the scripts executable: |
| 22 | + |
| 23 | +```bash |
| 24 | +chmod +x start-postgres.sh stop-postgres.sh status-postgres.sh |
| 25 | +``` |
| 26 | + |
| 27 | +### Starting PostgreSQL |
| 28 | + |
| 29 | +```bash |
| 30 | +./start-postgres.sh |
| 31 | +``` |
| 32 | + |
| 33 | +This script will: |
| 34 | +1. Start a PostgreSQL container |
| 35 | +2. Wait for it to be ready |
| 36 | +3. Display connection information |
| 37 | + |
| 38 | +### Checking Status |
| 39 | + |
| 40 | +```bash |
| 41 | +./status-postgres.sh |
| 42 | +``` |
| 43 | + |
| 44 | +This script will show if PostgreSQL is running and display connection details. |
| 45 | + |
| 46 | +### Stopping PostgreSQL |
| 47 | + |
| 48 | +```bash |
| 49 | +./stop-postgres.sh |
| 50 | +``` |
| 51 | + |
| 52 | +This script stops the PostgreSQL container while preserving your data. |
| 53 | + |
| 54 | +## Connection Information |
| 55 | + |
| 56 | +Once the database is running, you can connect to it using: |
| 57 | + |
| 58 | +- **Host**: localhost |
| 59 | +- **Port**: 5432 |
| 60 | +- **Username**: postgres |
| 61 | +- **Password**: postgres |
| 62 | +- **Database**: mydb |
| 63 | + |
| 64 | +### Connect with psql client: |
| 65 | + |
| 66 | +```bash |
| 67 | +psql -h localhost -U postgres -d mydb |
| 68 | +``` |
| 69 | + |
| 70 | +### Connect from applications: |
| 71 | + |
| 72 | +Connection string format: |
| 73 | +``` |
| 74 | +postgresql://postgres:postgres@localhost:5432/mydb |
| 75 | +``` |
| 76 | + |
| 77 | +## Customizing the Setup |
| 78 | + |
| 79 | +You can modify the `docker-compose.yml` file to change: |
| 80 | + |
| 81 | +- Database name, username, and password |
| 82 | +- Port mappings |
| 83 | +- Volume configuration |
| 84 | +- Add initialization scripts |
| 85 | + |
| 86 | +## Data Persistence |
| 87 | + |
| 88 | +The database data is stored in a Docker volume named `postgres_data` which persists between container restarts. To completely reset the data, you would need to remove this volume: |
| 89 | + |
| 90 | +```bash |
| 91 | +docker volume rm db_postgres_data |
| 92 | +``` |
| 93 | + |
| 94 | +## Troubleshooting |
| 95 | + |
| 96 | +If you encounter issues, check the container logs: |
| 97 | + |
| 98 | +```bash |
| 99 | +docker-compose logs postgres |
| 100 | +``` |
0 commit comments