How to Use CURRENT_USER in PostgreSQL

Galaxy Glossary

How do I use the PostgreSQL CURRENT_USER function?

CURRENT_USER returns the name of the role executing the current SQL statement.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

How do I get the current database user?

Call CURRENT_USER anywhere in a query to obtain the session’s role name. No arguments are required, and the value is constant for the duration of the statement.

What is the syntax of CURRENT_USER?

Use SELECT CURRENT_USER; in psql or embed the function inside larger queries, joins, or expressions. Alias it when needed for readability.

How can I see who inserted each order?

Create an created_by column and set its default to CURRENT_USER. Every new row then stores the login that executed the INSERT.

Code example

```sqlALTER TABLE Orders ADD COLUMN created_by text DEFAULT CURRENT_USER;```

Can I combine CURRENT_USER with other columns?

Yes. Mix it with regular columns to record both business and audit data in the same statement.

Auditing insert

```sqlINSERT INTO Orders (id, customer_id, order_date, total_amount, created_by)SELECT o.id, o.customer_id, NOW(), 99.95, CURRENT_USERFROM temp_new_orders o;```

Best practices for CURRENT_USER

Store the value in lowercase for consistent joins. Always grant roles instead of individual users so audit data remains stable after staff changes.

Common mistakes

Mixing CURRENT_USER with SET ROLE. After SET ROLE, CURRENT_USER keeps the original login role; use SESSION_USER if you need the pre-SET ROLE name.

Using CURRENT_USER in PL/pgSQL triggers without qualification. Inside a security-definer function, CURRENT_USER returns the definer, not the caller. Use current_setting('role') or SESSION_USER if you need the invoker.

Why How to Use CURRENT_USER in PostgreSQL is important

How to Use CURRENT_USER in PostgreSQL Example Usage


-- Show each order alongside the user who inserted it
SELECT id,
       customer_id,
       order_date,
       total_amount,
       created_by AS inserted_by
FROM   Orders
WHERE  created_by = CURRENT_USER;

How to Use CURRENT_USER in PostgreSQL Syntax


SELECT CURRENT_USER;
-- Alias for clarity
SELECT CURRENT_USER AS executing_user;

-- Use in a column list
INSERT INTO Orders (id, customer_id, order_date, total_amount, created_by)
VALUES (1, 10, NOW(), 59.99, CURRENT_USER);

-- Default value on a table
ALTER TABLE Orders
ADD COLUMN created_by text DEFAULT CURRENT_USER;

Common Mistakes

Frequently Asked Questions (FAQs)

Does CURRENT_USER require special privileges?

No. Every logged-in role can call the function without additional grants.

Is CURRENT_USER the same as USER?

Yes. PostgreSQL treats USER and CURRENT_USER as synonyms.

Can I cast CURRENT_USER?

Yes, e.g., (CURRENT_USER)::text to make the type explicit.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.