You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: If managed user creation/alteration fails for whatever reason, postgres logs the user's password #9799
I have searched for an existing issue, and could not find anything. I believe this is a new bug.
I have read the troubleshooting guide
I have read the troubleshooting guide and I think this is a new bug.
I am running a supported version of CloudNativePG
I have read the troubleshooting guide and I think this is a new bug.
Contact Details
No response
Version
1.28 (latest patch)
What version of Kubernetes are you using?
1.34
What is your Kubernetes environment?
Self-managed: RKE
How did you install the operator?
YAML manifest
What happened?
If managed user creation fails, postgres logs the query creating/altering the said user, containing the user's password. This is a major security risk, since access to logs is generally not as limited as to kubernetes secrets, where the password usually resides. Generally, applications should NEVER log passwords.
Cluster resource
Relevant log output
{"level":"info","ts":"2026-01-26T09:04:40.117706748Z","logger":"postgres","msg":"record","logging_pod":"a-database-cnpg-dp-1","record":{"log_time":"2026-01-26 09:04:40.117 UTC","user_name":"postgres","database_name":"postgres","process_id":"194974","connection_from":"[local]","session_id":"69772e28.2f99e","session_line_num":"1","command_tag":"ALTER ROLE","session_start_time":"2026-01-26 09:04:40 UTC","virtual_transaction_id":"22/2862","transaction_id":"0","error_severity":"ERROR","sql_state_code":"25006","message":"cannot execute ALTER ROLE in a read-only transaction","query":"ALTER ROLE \"dbadmin\" NOBYPASSRLS NOCREATEDB NOCREATEROLE INHERIT LOGIN NOREPLICATION SUPERUSER CONNECTION LIMIT -1 PASSWORD 'REDACTED123'","application_name":"cnpg-instance-manager","backend_type":"client backend","query_id":"0"}}
This is a real security risk: CloudNativePG logs SQL statements from PostgreSQL exactly as they are executed, including any passwords in queries like ALTER ROLE ... PASSWORD ... if an error occurs. There is no built-in mechanism in CloudNativePG or its logging configuration to redact or suppress sensitive data from these logs, and the PostgreSQL log settings are fixed and cannot be changed by users in CloudNativePG. The operator collects logs in CSV format from PostgreSQL and outputs them in JSON, but does not filter or modify the query text—so any password in the SQL will appear in the logs if the query fails or is logged for any reason. This behavior is not documented or addressed in existing issues or documentation (reference, reference).
Best practice is to strictly limit access to logs and avoid exposing them to anyone who should not see sensitive data. If you need to mitigate this, you may want to propose a feature to redact passwords in logs or investigate PostgreSQL-level options, but currently there is no workaround in CloudNativePG.
@dosu bad bot, we have multiple good (or at least better) options here than "strictly limit access to logs", which is an absurd statement to make. Logs are usually stored in plain-text, so they will always be vulnerable to someone staring at my screen if I'm reading them. Random log parsers can also leak this password to unimaginable places, so the "best practice" definitely isn't "keeping logs safe". Instead, I would suggest:
Use a prepared statement
Changing the loglevel for the user alterating transaction, which in theory is possible, like
BEGIN;
SET LOCAL log_min_error_statement = 'PANIC';
ALTER ROLE alice PASSWORD 'supersecret';
COMMIT;
Do the hashing beforehand, and only put the hash into the creation/alteration query, like
ALTER ROLE alice PASSWORD 'SCRAM-SHA-256$4096:...:...:...';
I'm by no means a postgresql expert, so the above solutions could carry other security implications that I'm not aware of, or could not be possible at all.
Also, arguing with an AI on an online forum was not on my agenda today...
You're right that there are better ways to mitigate password leakage in logs than just restricting log access. Here’s how your suggestions map to what’s possible in CloudNativePG and PostgreSQL:
Prepared statements: CloudNativePG does not use prepared statements for DDL like ALTER ROLE—it sends raw SQL via the Go database/sql API with the pgx driver. In PostgreSQL, prepared statements are not supported for DDL commands (including ALTER ROLE), so this approach wouldn’t prevent passwords from appearing in logs and isn’t implementable in this context [code reference].
Changing log level with SET LOCAL log_min_error_statement: CloudNativePG does not currently wrap role management operations in a transaction that changes the log level, nor does it issue SET LOCAL commands before running DDL. While this is theoretically possible in PostgreSQL, the operator does not implement it, and there’s no evidence in the code or docs that this is supported or planned. It would require significant changes to how SQL is generated and executed [code reference].
Pre-hashing passwords: This is fully supported and is the only currently available mitigation. If you provide a pre-hashed password (MD5 or SCRAM-SHA-256) in the Kubernetes Secret, CloudNativePG will use the hash directly in the SQL, so the plaintext password never appears in the query or logs. This is documented and works as you described [docs].
So, if you want to avoid password leakage in logs, the actionable solution right now is to always use pre-hashed passwords in your Secrets. The other options aren’t supported due to PostgreSQL and operator limitations. If you think log redaction or dynamic log level changes should be added, it would require a feature proposal and architectural changes.
This is a very good find, and a good bug report @lgyurci
I think storing the pass in SCRAM-SHA will be the way to go. I'll try to get a PR for this out this week.
EDIT: on reconsideration I too think log_min_error_statement is the better idea.
For the same reason you outlined, this is a problem for the roles created as part of cluster bootstrap (app and perhaps the superuser), so I'm going to apply the same fix with log_min_error_statement.
I have a PR going (linked above). I'm doing a couple of tests more, but I think it's almost ready.
@lgyurci the PR is ready. I have a question about your issue report. The root cause seems to be the role operation was executed in a read-only transaction:
cannot execute ALTER ROLE in a read-only transaction
There are very few places in the codebase where a read-only transaction is used.
What was going in your cluster at the time of this error?
Do you have a repro for the issue?
Ah, right! I saw that other issue but had not made the connection.
The fix in the PR would fix that other issue, but I think a separate fix might be in order to avoid executing the role reconciler in replica clusters. Ought to be a relatively easy fix, I'll see if I can find time soon.
Activity
dosubot commentedon Jan 26, 2026
This is a real security risk: CloudNativePG logs SQL statements from PostgreSQL exactly as they are executed, including any passwords in queries like
ALTER ROLE ... PASSWORD ...if an error occurs. There is no built-in mechanism in CloudNativePG or its logging configuration to redact or suppress sensitive data from these logs, and the PostgreSQL log settings are fixed and cannot be changed by users in CloudNativePG. The operator collects logs in CSV format from PostgreSQL and outputs them in JSON, but does not filter or modify the query text—so any password in the SQL will appear in the logs if the query fails or is logged for any reason. This behavior is not documented or addressed in existing issues or documentation (reference, reference).Best practice is to strictly limit access to logs and avoid exposing them to anyone who should not see sensitive data. If you need to mitigate this, you may want to propose a feature to redact passwords in logs or investigate PostgreSQL-level options, but currently there is no workaround in CloudNativePG.
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.
lgyurci commentedon Jan 26, 2026
@dosu bad bot, we have multiple good (or at least better) options here than "strictly limit access to logs", which is an absurd statement to make. Logs are usually stored in plain-text, so they will always be vulnerable to someone staring at my screen if I'm reading them. Random log parsers can also leak this password to unimaginable places, so the "best practice" definitely isn't "keeping logs safe". Instead, I would suggest:
I'm by no means a postgresql expert, so the above solutions could carry other security implications that I'm not aware of, or could not be possible at all.
Also, arguing with an AI on an online forum was not on my agenda today...
dosubot commentedon Jan 26, 2026
You're right that there are better ways to mitigate password leakage in logs than just restricting log access. Here’s how your suggestions map to what’s possible in CloudNativePG and PostgreSQL:
Prepared statements: CloudNativePG does not use prepared statements for DDL like
ALTER ROLE—it sends raw SQL via the Godatabase/sqlAPI with the pgx driver. In PostgreSQL, prepared statements are not supported for DDL commands (includingALTER ROLE), so this approach wouldn’t prevent passwords from appearing in logs and isn’t implementable in this context [code reference].Changing log level with
SET LOCAL log_min_error_statement: CloudNativePG does not currently wrap role management operations in a transaction that changes the log level, nor does it issueSET LOCALcommands before running DDL. While this is theoretically possible in PostgreSQL, the operator does not implement it, and there’s no evidence in the code or docs that this is supported or planned. It would require significant changes to how SQL is generated and executed [code reference].Pre-hashing passwords: This is fully supported and is the only currently available mitigation. If you provide a pre-hashed password (MD5 or SCRAM-SHA-256) in the Kubernetes Secret, CloudNativePG will use the hash directly in the SQL, so the plaintext password never appears in the query or logs. This is documented and works as you described [docs].
So, if you want to avoid password leakage in logs, the actionable solution right now is to always use pre-hashed passwords in your Secrets. The other options aren’t supported due to PostgreSQL and operator limitations. If you think log redaction or dynamic log level changes should be added, it would require a feature proposal and architectural changes.
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.
ardentperf commentedon Jan 31, 2026
CNPG wrapping role management with
log_min_error_statementis an interesting idea - this might workjsilvela commentedon Feb 9, 2026
This is a very good find, and a good bug report @lgyurci
I think storing the pass in SCRAM-SHA will be the way to go. I'll try to get a PR for this out this week.
EDIT: on reconsideration I too think
log_min_error_statementis the better idea.jsilvela commentedon Feb 10, 2026
For the same reason you outlined, this is a problem for the roles created as part of cluster bootstrap (
appand perhaps the superuser), so I'm going to apply the same fix withlog_min_error_statement.I have a PR going (linked above). I'm doing a couple of tests more, but I think it's almost ready.
jsilvela commentedon Feb 11, 2026
@lgyurci the PR is ready. I have a question about your issue report. The root cause seems to be the role operation was executed in a read-only transaction:
There are very few places in the codebase where a read-only transaction is used.
What was going in your cluster at the time of this error?
Do you have a repro for the issue?
lgyurci commentedon Feb 11, 2026
I even made another issue for it: #9800
EDIT: thanks for your work so far!
jsilvela commentedon Feb 11, 2026
Ah, right! I saw that other issue but had not made the connection.
The fix in the PR would fix that other issue, but I think a separate fix might be in order to avoid executing the role reconciler in replica clusters. Ought to be a relatively easy fix, I'll see if I can find time soon.
jsilvela commentedon Feb 11, 2026
Direct verification of the fix on a replica cluster, duplicating your reported conditions:
{"level":"info","ts":"2026-02-11T13:57:39.049732289Z", "logger":"postgres","msg":"record","logging_pod":"cluster-replica-tls-1", "record":{"log_time":"2026-02-11 13:57:39.049 UTC", "user_name":"postgres","database_name":"postgres","process_id":"156","connection_from":"[local]", "session_id":"698c8ad3.9c","session_line_num":"1", "command_tag":"ALTER ROLE","session_start_time":"2026-02-11 13:57:39 UTC", "virtual_transaction_id":"26/2","transaction_id":"0", "error_severity":"ERROR", "sql_state_code":"25006", "message":"cannot execute ALTER ROLE in a read-only transaction", "application_name":"cnpg-instance-manager","backend_type":"client backend", "query_id":"0"} }fix(roles): prevent postgres from leaking role passwords in logs (#9950)
fix(roles): prevent postgres from leaking role passwords in logs (#9950)
fix(roles): prevent postgres from leaking role passwords in logs (#9950)
fix(roles): prevent postgres from leaking role passwords in logs (clo…