PostgreSQL – How to Alter Password from Python

postgresqlpython

When I run this query from psql shell everything works fine:

ALTER ROLE postgres WITH ENCRYPTED PASSWORD 'somePassword';

but when I try exactly the same query from python (with psycopg2 package)
authentication fails when I try to connect with the password.

what can go wrong? I tried simple passwords with no non-ansi characters etc…

Best Answer

You have two options when running code from Python,

  1. Python has a method called autocommit you either have to turn this on,
  2. or, you have to run a manual COMMIT; after you issue the command.

Without either of those the command will automatically ROLLBACK;

answer inspired by @a_horse_with_no_name in the comments