I'm currently using Neon Postgres, which works perfectly fine, when I attempt to connect it directly from Windows.
However, as soon as I go to WSL, and try to connect using the pg
client, I get the following error:
at internalConnectMultiple (node:net:1116:18)
at internalConnectMultiple (node:net:1184:5)
at Timeout.internalConnectMultipleTimeout (node:net:1710:5)
at listOnTimeout (node:internal/timers:575:11)
at process.processTimers (node:internal/timers:514:7) {
code: 'ETIMEDOUT',
[errors]: [
Error: connect ETIMEDOUT 3.131.64.200:5432
at createConnectionError (node:net:1646:14)
at Timeout.internalConnectMultipleTimeout (node:net:1705:38)
at listOnTimeout (node:internal/timers:575:11)
at process.processTimers (node:internal/timers:514:7) {
errno: -110,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '3.131.64.200',
port: 5432
},
Error: connect ENETUNREACH 2600:1f16:12b2:b416:3645:2f59:5554:9b89:5432 - Local (:::0)
at internalConnectMultiple (node:net:1180:16)
at Timeout.internalConnectMultipleTimeout (node:net:1710:5)
at listOnTimeout (node:internal/timers:575:11)
at process.processTimers (node:internal/timers:514:7) {
errno: -101,
code: 'ENETUNREACH',
syscall: 'connect',
address: '2600:1f16:12b2:b416:3645:2f59:5554:9b89',
port: 5432
},
Error: connect ETIMEDOUT 3.143.47.40:5432
at createConnectionError (node:net:1646:14)
at Timeout.internalConnectMultipleTimeout (node:net:1705:38)
at listOnTimeout (node:internal/timers:575:11)
at process.processTimers (node:internal/timers:514:7) {
errno: -110,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '3.143.47.40',
port: 5432
},
Error: connect ENETUNREACH 2600:1f16:12b2:b424:fdaf:6c84:8d6e:429f:5432 - Local (:::0)
at internalConnectMultiple (node:net:1180:16)
at Timeout.internalConnectMultipleTimeout (node:net:1710:5)
at listOnTimeout (node:internal/timers:575:11)
at process.processTimers (node:internal/timers:514:7) {
errno: -101,
code: 'ENETUNREACH',
syscall: 'connect',
address: '2600:1f16:12b2:b424:fdaf:6c84:8d6e:429f',
port: 5432
},
Error: connect ETIMEDOUT 3.23.186.13:5432
at createConnectionError (node:net:1646:14)
at Timeout.internalConnectMultipleTimeout (node:net:1705:38)
at listOnTimeout (node:internal/timers:575:11)
at process.processTimers (node:internal/timers:514:7) {
errno: -110,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '3.23.186.13',
port: 5432
},
Error: connect ENETUNREACH 2600:1f16:12b2:b40a:533d:22ad:38c2:f393:5432 - Local (:::0)
at internalConnectMultiple (node:net:1180:16)
at Timeout.internalConnectMultipleTimeout (node:net:1710:5)
at listOnTimeout (node:internal/timers:575:11)
at process.processTimers (node:internal/timers:514:7) {
errno: -101,
code: 'ENETUNREACH',
syscall: 'connect',
address: '2600:1f16:12b2:b40a:533d:22ad:38c2:f393',
port: 5432
}
]
}
How can I fix this?
Edit: I've attached the code as requested
const { Client } = require('pg');
require('dotenv').config();
async function testDB() {
console.log('Starting database test...');
console.log('Connection string:', process.env.DATABASE_URL);
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
try {
console.log('Connecting to database...');
await client.connect();
console.log('Connected successfully!');
// Test a simple query
const result = await client.query('SELECT NOW()');
console.log('Query result:', result.rows[0]);
console.log('Test completed successfully!');
} catch (err) {
console.error('Test failed:', err);
} finally {
await client.end();
}
}
// Run the test
testDB();
So the cause of this solution was ipv6, if I just run:
sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.lo.disable_ipv6=1
It can connect to the remote DB, but I just can't access the web application running on WSL from my local system for some reason, so this is not a suitable solution.
In case the problem is the networking between WSL and a host, as the updated question suggests, "mirrored" networking mode can be used to eliminate it:
On machines running Windows 11 22H2 and higher you can set networkingMode=mirrored under [wsl2] in the .wslconfig file to enable mirrored mode networking. Enabling this changes WSL to an entirely new networking architecture which has the goal of 'mirroring' the network interfaces that you have on Windows into Linux, to add new networking features and improve compatibility.