SQL Keywords

SQL CURRENT_USER

What is SQL CURRENT_USER?

Returns the authorization identifier (user name) of the session that is currently executing the SQL statement.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

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

Compatible dialects for SQL CURRENT_USER: Supported in: PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, Snowflake, DB2, SQLite (as USER function alias), and most standards-compliant engines.

SQL CURRENT_USER Full Explanation

CURRENT_USER is a zero-argument scalar function defined in the SQL standard that exposes the name of the user currently authenticated to the database. The value is evaluated at statement start and remains constant for the duration of that statement. It reflects the effective user after privilege escalation mechanisms such as SET ROLE in PostgreSQL and Oracle. Because it is context sensitive, it cannot be indexed and is resolved at runtime. CURRENT_USER is often used for auditing, row-level security, and conditional logic. In some databases (e.g., MySQL), the parentheses form CURRENT_USER() is also accepted, but the keyword alone is sufficient and preferred for portability.

SQL CURRENT_USER Syntax

SELECT CURRENT_USER;

SQL CURRENT_USER Parameters

Example Queries Using SQL CURRENT_USER

-- 1. Quick check of the connected user
SELECT CURRENT_USER;

-- 2. Store the runner of a batch job
INSERT INTO job_log(run_by, run_at)
VALUES (CURRENT_USER, CURRENT_TIMESTAMP);

-- 3. Compare session and current user after SET ROLE (PostgreSQL)
BEGIN;
  SET ROLE reporting_role;
  SELECT SESSION_USER AS login_user,
         CURRENT_USER  AS active_user;
ROLLBACK;

-- 4. Use in a view for automatic auditing
CREATE OR REPLACE VIEW sales_vw AS
SELECT *, CURRENT_USER AS queried_by
FROM sales;

Expected Output Using SQL CURRENT_USER

  • Each query returns a single text value containing the user name (e
  • g
  • , "alice") or uses that value in INSERTs, comparisons, or views
  • The third example shows different values for SESSION_USER and CURRENT_USER after SET ROLE

Use Cases with SQL CURRENT_USER

  • Add who-ran-this metadata in audit tables
  • Build views that expose the querying user for downstream ETL jobs
  • Implement row-level security policies that filter rows by CURRENT_USER
  • Debug privilege escalation by comparing SESSION_USER and CURRENT_USER

Common Mistakes with SQL CURRENT_USER

  • Adding parentheses in databases that do not allow CURRENT_USER() (SQL Server, Oracle)
  • Confusing CURRENT_USER with SESSION_USER or SYSTEM_USER, which can differ after SET ROLE or EXECUTE AS
  • Expecting CURRENT_USER to change inside a single statement after a nested privilege change
  • Using it in indexed or persisted computed columns (value is nondeterministic across sessions)

Related Topics

SESSION_USER, CURRENT_ROLE, SYSTEM_USER, USER, CURRENT_SCHEMA, CURRENT_CATALOG

First Introduced In

SQL-92

Frequently Asked Questions

What is the difference between CURRENT_USER and SESSION_USER?

CURRENT_USER reflects the active user after privilege changes such as SET ROLE, while SESSION_USER always shows who originally connected.

Does CURRENT_USER update inside a transaction?

Yes, if you run SET ROLE or EXECUTE AS inside the same transaction, CURRENT_USER will return the new effective user in subsequent statements. The value is fixed only for the individual statement, not the whole transaction.

Is CURRENT_USER available in MySQL?

Yes. MySQL supports both CURRENT_USER and CURRENT_USER(). The function returns 'user_name@host' rather than just the user name.

How can I use CURRENT_USER for row level security?

Create a policy or view that filters rows where owner = CURRENT_USER. The database engine will automatically substitute the current session's user every time the query runs.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!