Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgres: What is the query 'select * from user' actually doing?

Tags:

sql

postgresql

In psql, if one types 'select * from user' you'll get something like the following back:

 current_user 
--------------
 postgres

What is user in this context?

like image 238
rpkelly Avatar asked Jan 25 '12 21:01

rpkelly


People also ask

How does select query work in PostgreSQL?

The PostgreSQL SELECT statement retrieves data from a single or several tables in a database, and returns the data in a result table, called a result-set. Use the SELECT statement to return one or more rows matching the specified criteria from the database tables.

What is the wildcard in PostgreSQL?

Wildcards in PostgreSQL is used to find matching rows values from tables; it is also used to find matching patterns rows from tables, Wildcards is also used to find matching rows, column and tables names; the output of the wildcard operator will return matching name, which was table name, column name or rows, In ...

How do I get unique values from a column in PostgreSQL?

Removing duplicate rows from a query result set in PostgreSQL can be done using the SELECT statement with the DISTINCT clause. It keeps one row for each group of duplicates. The DISTINCT clause can be used for a single column or for a list of columns.

How do I assign a select query to a variable in PostgreSQL?

In PostgreSQL, the select into statement to select data from the database and assign it to a variable. Syntax: select select_list into variable_name from table_expression; In this syntax, one can place the variable after the into keyword.


3 Answers

In this context, user is a reserved internal Postgres function that represents the current user logged in to the database.

This query can also be written as:

SELECT user;

Which should yield the same thing. Note, if you want to actually reference or create a table named user you'll have to use quotes, or fully qualify the schema it lives in. For example:

CREATE TABLE "user"
(
  id int2 not null
);

will work but:

CREATE TABLE user
(
  id int2 not null
);

Will yield an error.

Here's a reference for other system information functions:

http://www.postgresql.org/docs/9.0/static/functions-info.html

like image 200
Mike Christensen Avatar answered Oct 24 '22 07:10

Mike Christensen


See the Postgresql Documentation on system functions.

Basically "select * from user" is one of the Postgresql-specific ways of finding the current user. It is functionally the same as using the current_user function eg: "select current_user()".

Other special functions that can be used as tables in queries include:

current_catalog
current_schema
like image 22
Gary Avatar answered Oct 24 '22 08:10

Gary


If You are looking for list of users i should seek in pg_user table; SELECT * FROM pg_user;

Your Query get all data from result of special function named user. That function returns username of current_user.

like image 37
i2i Avatar answered Oct 24 '22 09:10

i2i