How to CURRENT_USER in PostgreSQL

Galaxy Glossary

How do I use the CURRENT_USER function in PostgreSQL?

CURRENT_USER returns the name of the role executing the current transaction.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Description

Table of Contents

What does CURRENT_USER return?

CURRENT_USER returns the role name that owns the current database session.It is evaluated once per transaction and is not affected by SET ROLE.

When should I use CURRENT_USER?

Use it in audit tables, row-level security (RLS) policies, or dynamic SQL that must adapt permissions to the executing role.

How do I call CURRENT_USER in a SELECT?

Place CURRENT_USER wherever an expression is valid: SELECT CURRENT_USER; or add it as a column in a larger query.

Can I alias CURRENT_USER?

Yes.SELECT CURRENT_USER AS session_user; is common when logging user activity.

How does CURRENT_USER differ from SESSION_USER?

SESSION_USER returns the role that authenticated the connection.CURRENT_USER switches when you execute SET ROLE; SESSION_USER does not.

Example: stamping orders with the session role

Insert the current role into an audit trail: INSERT INTO order_audit(order_id, changed_by) VALUES (42, CURRENT_USER);

Example: row-level security policy

CREATE POLICY customer_is_owner ON Orders USING (customer_id = (SELECT id FROM Customers WHERE email = CURRENT_USER));

Best practices

1) Cast CURRENT_USER to text if concatenating. 2) Avoid relying on it for security without RLS or GRANTs. 3) Log it early in triggers for traceability.

.

Why How to CURRENT_USER in PostgreSQL is important

How to CURRENT_USER in PostgreSQL Example Usage


-- Who is running this?
SELECT CURRENT_USER AS running_role;

-- Audit an order update
UPDATE Orders SET total_amount = 129.99 WHERE id = 101;
-- Trigger logs: (order_id=101, changed_by='dev_user', changed_at=NOW())

How to CURRENT_USER in PostgreSQL Syntax


-- Basic usage
SELECT CURRENT_USER;

-- Alias for readability
SELECT CURRENT_USER AS session_role;

-- Use in INSERT (ecommerce)
INSERT INTO Orders(id, customer_id, order_date, total_amount, created_by)
VALUES (101, 7, NOW(), 149.99, CURRENT_USER);

-- Use in a function
CREATE OR REPLACE FUNCTION log_order_update() RETURNS trigger AS $$
BEGIN
  INSERT INTO order_audit(order_id, changed_by, changed_at)
  VALUES (NEW.id, CURRENT_USER, NOW());
  RETURN NEW;
END;$$ LANGUAGE plpgsql;

Common Mistakes

Frequently Asked Questions (FAQs)

Does CURRENT_USER require parentheses?

No. It is a special SQL keyword, not a function call.

Is CURRENT_USER immutable?

Yes within a transaction; it resets only after a new transaction starts.

Can I grant permissions based on CURRENT_USER?

Use row-level security policies referencing CURRENT_USER for fine-grained access.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.