This commit is contained in:
Andrey Kondratev
2025-08-29 14:49:40 +05:00
parent 816b0ec672
commit 45a071ab4f
2 changed files with 74 additions and 25 deletions

View File

@@ -0,0 +1,33 @@
# Database Authentication Issue Fixed
## Problem
- PostgreSQL authentication was failing with error code 28P01 "password authentication failed for user 'quixotic'"
- The docker-compose.yml had default password `quixotic123` but .env.docker had different password `GGkM0a6r7pw3F65ZvYbbuOgbt7gO2AoW3JynEk7m9Go=`
- Secondary issue: table creation conflicts due to SERIAL sequences being created multiple times
## Solution
1. **Password sync**: Removed postgres container and volume to reset with correct password from .env.docker
```bash
docker compose down postgres
docker volume rm quixotic_postgres-data
docker compose up postgres -d
```
2. **Table creation fix**: Modified database.ts to check table existence before creating:
- Added proper table existence check using information_schema
- Removed `CREATE TABLE IF NOT EXISTS` which wasn't handling SERIAL sequences properly
- Added logging for better debugging
## Commands used
```bash
docker compose down postgres
docker volume rm quixotic_postgres-data
docker compose up postgres -d
docker compose build quixotic-app
docker compose up quixotic-app -d
```
## Result
- Database authentication working
- No more table creation conflicts
- App starts successfully with "Database tables already exist" message

View File

@@ -30,8 +30,19 @@ export class Database {
private async init(): Promise<void> {
try {
const tablesExist = await this.pool.query(`
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'users'
);
`);
if (!tablesExist.rows[0].exists) {
console.log('Creating database tables...');
// Users table
await this.pool.query(`CREATE TABLE IF NOT EXISTS users (
await this.pool.query(`CREATE TABLE users (
id SERIAL PRIMARY KEY,
telegram_id BIGINT UNIQUE NOT NULL,
username TEXT,
@@ -41,7 +52,7 @@ export class Database {
)`);
// Search history table
await this.pool.query(`CREATE TABLE IF NOT EXISTS search_history (
await this.pool.query(`CREATE TABLE search_history (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
query TEXT NOT NULL,
@@ -49,7 +60,7 @@ export class Database {
)`);
// Downloaded files table
await this.pool.query(`CREATE TABLE IF NOT EXISTS downloads (
await this.pool.query(`CREATE TABLE downloads (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
track_id TEXT NOT NULL,
@@ -57,6 +68,11 @@ export class Database {
file_path TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)`);
console.log('Database tables created successfully');
} else {
console.log('Database tables already exist');
}
} catch (error) {
console.error('Database initialization error:', error);
}