your code is straightforward, so the issue isn’t in the Python logic itself.
When this exact script is frozen into an .exe with PyInstaller, there are a few gotchas that apply to psycopg2 and PostgreSQL connections on Windows.
https://www.instagram.com/aniket_p_vitkar/