curl -X POST http://localhost:3000/api/test-connection \
-H "Content-Type: application/json" \
-d '{
"host": "localhost",
"port": 5432,
"user": "postgres",
"password": "mypassword",
"database": "mydb",
"sslMode": "disable"
}'
{
"success": true,
"message": "Connection successful!"
}
Verify PostgreSQL database credentials and connectivity
curl -X POST http://localhost:3000/api/test-connection \
-H "Content-Type: application/json" \
-d '{
"host": "localhost",
"port": 5432,
"user": "postgres",
"password": "mypassword",
"database": "mydb",
"sslMode": "disable"
}'
{
"success": true,
"message": "Connection successful!"
}
localhost, db.example.com, 192.168.1.1005432postgres if not specifieddisable, require, preferDefault: disablefalse for errorscurl -X POST http://localhost:3000/api/test-connection \
-H "Content-Type: application/json" \
-d '{
"host": "localhost",
"port": 5432,
"user": "postgres",
"password": "mypassword",
"database": "mydb",
"sslMode": "disable"
}'
{
"success": true,
"message": "Connection successful!"
}
curl -X POST http://localhost:3000/api/test-connection \
-H "Content-Type: application/json" \
-d '{
"host": "localhost",
"port": 5433,
"user": "postgres",
"password": "mypassword"
}'
{
"success": false,
"error": "Connection refused",
"details": "Unable to connect to the database server. Please check if the server is running and the host/port are correct.",
"code": "ECONNREFUSED"
}
{
"success": false,
"error": "Authentication failed",
"details": "Invalid username or password.",
"code": "28P01"
}
{
"success": false,
"error": "Database does not exist",
"details": "The specified database does not exist on the server. Try connecting without specifying a database or use a different database name.",
"code": "3D000"
}
{
"success": false,
"error": "Missing required fields: host, port, and user are required"
}
| Code | Error | Description |
|---|---|---|
ECONNREFUSED | Connection refused | Server is not running or port is incorrect |
ENOTFOUND | Host not found | Invalid hostname or DNS resolution failed |
ECONNRESET | Connection reset | SSL configuration mismatch |
28P01 | Authentication failed | Invalid username or password |
3D000 | Database does not exist | Specified database not found |
28000 | Invalid authorization | User lacks database access permissions |
/app/api/test-connection/route.ts:15-30:
const client = new Client({
host,
port,
user,
password,
database: database || 'postgres', // Use postgres as default
ssl: sslMode === "require" ? { rejectUnauthorized: false } : false,
connectionTimeoutMillis: 5000, // 5 second timeout
})
await client.connect()
await client.query('SELECT 1') // Validate connection
await client.end()