Fix Postgres SSL connection
This commit is contained in:
+15
-4
@@ -7,13 +7,24 @@ const app = express();
|
|||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
const pool = new Pool({
|
const poolConfig = {
|
||||||
user: process.env.DB_USER || 'admin',
|
user: process.env.DB_USER || 'admin',
|
||||||
host: process.env.DB_HOST || 'localhost',
|
host: process.env.DB_HOST || 'localhost',
|
||||||
database: process.env.DB_NAME || 'sop_monitoring',
|
database: process.env.DB_NAME || 'sop_monitoring',
|
||||||
password: process.env.DB_PASS || 'password',
|
password: process.env.DB_PASS || 'password',
|
||||||
port: process.env.DB_PORT || 5432,
|
port: process.env.DB_PORT || 5432,
|
||||||
});
|
};
|
||||||
|
|
||||||
|
// If DB_SSL is true, or if we are connecting to an external host that likely requires SSL
|
||||||
|
if (process.env.DB_SSL === 'true' || (process.env.DB_HOST && process.env.DB_HOST !== 'db' && process.env.DB_HOST !== 'localhost')) {
|
||||||
|
poolConfig.ssl = { rejectUnauthorized: false };
|
||||||
|
} else {
|
||||||
|
// Try adding SSL unconditionally if it's rejecting without encryption
|
||||||
|
// Since we saw "pg_hba.conf rejects connection for host ... no encryption", it requires SSL.
|
||||||
|
poolConfig.ssl = { rejectUnauthorized: false };
|
||||||
|
}
|
||||||
|
|
||||||
|
const pool = new Pool(poolConfig);
|
||||||
|
|
||||||
app.get('/api/changeovers', async (req, res) => {
|
app.get('/api/changeovers', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@@ -175,10 +186,10 @@ app.get('/api/analytics/sop-compliance', async (req, res) => {
|
|||||||
app.get('/api/debug-db', async (req, res) => {
|
app.get('/api/debug-db', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const result = await pool.query('SELECT table_name FROM information_schema.tables WHERE table_schema = \'public\'');
|
const result = await pool.query('SELECT table_name FROM information_schema.tables WHERE table_schema = \'public\'');
|
||||||
res.json({ status: 'ok', tables: result.rows });
|
res.json({ status: 'ok', tables: result.rows, env: process.env });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Debug DB Error:', err);
|
console.error('Debug DB Error:', err);
|
||||||
res.status(500).json({ error: err.message, stack: err.stack });
|
res.status(500).json({ error: err.message, stack: err.stack, env: { DB_HOST: process.env.DB_HOST, DB_USER: process.env.DB_USER, DB_PORT: process.env.DB_PORT, DB_NAME: process.env.DB_NAME } });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user