How to reset password for users in PostgreSQL database

AI-generated summary

The discussion revolves around resetting passwords for users in a PostgreSQL database for a Discourse application. Li_Bruce initially asked how to reset user passwords, specifically inquiring about the password encryption method used (MD5 or SHA256) and if it’s possible to update the password using a direct SQL query.

However, pfaffman suggested an alternative approach, advising against manual password resets and instead recommended using the built-in password reset link. If a manual reset is necessary, pfaffman proposed using the Rails console.

supermathie provided a code snippet for updating a user’s password using the Rails console. Li_Bruce adapted this solution, successfully resetting a user’s password using the command: RAILS_ENV=production rails runner 'User.find_by_email("admin@example.com").update!(password:"AdminAdmin1234566xxxxxx")'

Hello everybody,

We have some questions about how to reset password for users in discourse.
We know that the command “RAILS_ENV=production /usr/local/bin/bundle exec rake admin:create” can reset the password for existing users or create a new admin user. But we do not want to reset password for admin user using this method. We want to reset password for users in PostgreSQL database.
The user information is stored in users table in postgresql database. And we can check the users and password using “select id, username, name, password_hash from users;”. So we have two questions.

  1. How does the password encrypt? MD5 ? Sha256?
  2. Can we reset password using " UPDATE users SET password_hash = ‘there is a hash password’ WHERE id=1; " ?

If someone has an idea it would be really helpful, thank you!

What problem are you solving? Why not have them use the reset password link and set it themselves?

I would do it from the rails console rather than sql. I would have to look at the source to get the code.

From the rails console:

User.find_by(username: 'gollum').update!(password: 'shiiiire!-BAGGINS!')

@supermathie , thank you very much!
We can reset password by

RAILS_ENV=production rails runner 'User.find_by_email("admin@example.com").update!(password:"AdminAdmin1234566xxxxxx")'