CURRENT_USER

Returns the details (username and hostname) of the user executing the query.

Syntax

CURRENT_USER()
SESSION_USER()
USER()

Return Type

String

Remarks

  • USER() returns the hostname (if found) of the host specified to the client when connecting, or an empty string if that cannot be resolved.

  • CURRENT_USER() returns the authenticated host, which is taken from the matching user.

    CREATE USER 'admin'@'192.168.88.65';
    Query OK, 0 rows affected (0.003 sec)
    memsql -u admin -h 192.168.88.65
    SELECT USER(), CURRENT_USER();
    +--------+---------------------+
    | USER() | CURRENT_USER()      |
    +--------+---------------------+
    | admin@ | admin@192.168.88.65 |
    +--------+---------------------+
    1 row in set (0.042 sec)
  • SESSION_USER() is similar to CURRENT_USER(), but if called from inside a stored procedure, it returns the original user instead of the user the stored procedure is running as.

Examples

SELECT CURRENT_USER();
+----------------+
| CURRENT_USER() |
+----------------+
| root@%         |
+----------------+

The below query returns the current user name and host name for the connection.

SELECT USER();
+------------------------------+
| USER()                       |
+------------------------------+
| root@localhost               |
+------------------------------+
1 row in set (1.22 sec)

This feature can be combined with views to enable row-level permissions. In this example, persons with the employee role can only see themselves, while managers can see all records:

CREATE TABLE employees (
-> id int primary key,
-> fullname varchar(64),
-> account_name varchar(64),
-> role enum('employee', 'manager'),
-> salary int
-> );
CREATE TABLE permissions (
-> account_name varchar(64),
-> emp_id int,
-> primary key (account_name, emp_id)
-> );
CREATE VIEW employee_view as
-> select e.* from employees e, permissions p
-> where e.id = p.emp_id
-> and p.account_name = current_user();
INSERT INTO employees values
-> (1, 'SYSTEM', 'root@%', 'manager', 0),
-> (2, 'Kevin Kelvin', 'kelvin@%', 'manager', 5000),
-> (3, 'Oliver Heavyside', 'oliver@%', 'employee', 3000);
INSERT INTO permissions values
-> ('root@%', 1), ('root@%', 2), ('root@%', 3),
-> ('kelvin@%', 2), ('kelvin@%', 3),
-> ('oliver@%', 3);
-- as root:
SELECT CURRENT_USER(), * FROM employee_view;
+----------------+----+------------------+--------------+----------+--------+
| CURRENT_USER() | id | fullname         | account_name | role     | salary |
+----------------+----+------------------+--------------+----------+--------+
| root@%         |  1 | SYSTEM           | root@%       | manager  |      0 |
| root@%         |  2 | Kevin Kelvin     | kevin@%      | manager  |   5000 |
| root@%         |  3 | Oliver Heavyside | oliver@%     | employee |   3000 |
+----------------+----+------------------+--------------+----------+--------+
-- as kelvin:
SELECT CURRENT_USER(), * FROM employee_view;
+----------------+----+------------------+--------------+----------+--------+
| CURRENT_USER() | id | fullname         | account_name | role     | salary |
+----------------+----+------------------+--------------+----------+--------+
| kelvin@%       |  2 | Kevin Kelvin     | kevin@%      | manager  |   5000 |
| kelvin@%       |  3 | Oliver Heavyside | oliver@%     | employee |   3000 |
+----------------+----+------------------+--------------+----------+--------+
-- as oliver:
SELECT CURRENT_USER(), * FROM employee_view;
+----------------+----+------------------+--------------+----------+--------+
|CURRENT_USER()  | id | fullname         | account_name | role     | salary |
+----------------+----+------------------+--------------+----------+--------+
| oliver@%       |  3 | Oliver Heavyside | oliver@%     | employee |   3000 |
+----------------+----+------------------+--------------+----------+--------+

Last modified: August 18, 2023

Was this article helpful?

Verification instructions

Note: You must install cosign to verify the authenticity of the SingleStore file.

Use the following steps to verify the authenticity of singlestoredb-server, singlestoredb-toolbox, singlestoredb-studio, and singlestore-client SingleStore files that have been downloaded.

You may perform the following steps on any computer that can run cosign, such as the main deployment host of the cluster.

  1. (Optional) Run the following command to view the associated signature files.

    curl undefined
  2. Download the signature file from the SingleStore release server.

    • Option 1: Click the Download Signature button next to the SingleStore file.

    • Option 2: Copy and paste the following URL into the address bar of your browser and save the signature file.

    • Option 3: Run the following command to download the signature file.

      curl -O undefined
  3. After the signature file has been downloaded, run the following command to verify the authenticity of the SingleStore file.

    echo -n undefined |
    cosign verify-blob --certificate-oidc-issuer https://oidc.eks.us-east-1.amazonaws.com/id/CCDCDBA1379A5596AB5B2E46DCA385BC \
    --certificate-identity https://kubernetes.io/namespaces/freya-production/serviceaccounts/job-worker \
    --bundle undefined \
    --new-bundle-format -
    Verified OK