Rails
PostgreSQL

rails db psql コマンドメモ(個人用)

version check

select version();

start postgresql dialog mode

psql -d

show user list

\du

show db list

\l

connect db

\connect db_name

create user

\password username

select db (show current connection)

\c

select rows from table_name

select * from users;

usage of "order by"

select * from users order by id asc;
(select * from users order by idと同じ)

select * from users order by id desc;

usage of "limit"

select * from users order by id limit 2;

usage of "order by" & "limit"

select * from users order by id desc limit 2;

usage of "offset"

select * from users offset 2;
※offsetはindexとして捉える

usage of "distinct"

select distinct id from users;

usage of "sum"

select sum(id) from users;

usage of "max", "min"

select max(id) from users;
select min(id) from users;

usage of "where"

select * from users where id = 1;