65 lines
1.3 KiB
Markdown
65 lines
1.3 KiB
Markdown
# How-to: Connettersi a PostgreSQL in Docker
|
|
|
|
## Come connettersi al database
|
|
|
|
### Da container nella stessa rete
|
|
|
|
```bash
|
|
docker run --rm --network lab05-vpc-private \
|
|
-e PGPASSWORD=lab05_password \
|
|
postgres:16-alpine \
|
|
psql -h db -U lab05_user -d lab05_db
|
|
```
|
|
|
|
### Dall'host con port forwarding (non recommended)
|
|
|
|
Se devi connetterti dall'host, aggiungi temporaneamente:
|
|
|
|
```yaml
|
|
# docker-compose.yml
|
|
ports:
|
|
- "127.0.0.1:5432:5432"
|
|
```
|
|
|
|
Poi:
|
|
|
|
```bash
|
|
psql -h 127.0.0.1 -U lab05_user -d lab05_db
|
|
```
|
|
|
|
### Con pgAdmin o altri tool
|
|
|
|
1. Configura connection string:
|
|
```
|
|
host=127.0.0.1 port=5432 user=lab05_user password=lab05_password dbname=lab05_db
|
|
```
|
|
|
|
2. Oppure usa PostgreSQL URI:
|
|
```
|
|
postgresql://lab05_user:lab05_password@127.0.0.1:5432/lab05_db
|
|
```
|
|
|
|
## Risoluzione problemi
|
|
|
|
### Connection refused
|
|
|
|
Il database è in rete privata. Devi connetterti da container nella stessa rete:
|
|
|
|
```bash
|
|
# Client temporaneo nella rete privata
|
|
docker run --rm -it --network lab05-vpc-private \
|
|
-e PGPASSWORD=lab05_password \
|
|
postgres:16-alpine \
|
|
psql -h db -U lab05_user -d lab05_db
|
|
```
|
|
|
|
Nota: il servizio `lab05-app` usa `nginx:alpine` e non include `psql`.
|
|
|
|
### Password authentication failed
|
|
|
|
Verifica le credenziali in docker-compose.yml:
|
|
|
|
```bash
|
|
grep POSTGRES_PASSWORD docker-compose.yml
|
|
```
|