Acess The Postgresql
Acess The Postgresql
psql -U [username];
Code language: CSS (css)
For example, the following command uses the postgres user to access the PostgreSQL database server:
psql -U postgres
Code language: SQL (Structured Query Language) (sql)z
\c database_name;
\c dvdrental;
You are now connected to database "dvdrental" as user "postgres".
Code language: SQL (Structured Query Language) (sql)
\q
\l
\dn
Code language: SQL (Structured Query Language) (sql)
\df
\dv
Code language: SQL (Structured Query Language) (sql)
\dt
\d+ table_name
\df+ function_name
\x
\du
Managing databases
Managing tables
Create a new table or a temporary table
Rename a column:
ALTER TABLE table_name ALTER COLUMN [SET DEFAULT value | DROP DEFAULT]
Code language: SQL (Structured Query Language) (sql)
Rename a table.
Managing views
Create a view:
Drop a view:
Rename a view:
Managing indexes
SELECT column_list
FROM table;
Code language: SQL (Structured Query Language) (sql)
SELECT *
FROM table
WHERE condition;
Code language: SQL (Structured Query Language) (sql)
Query data from multiple using the inner join, left join, full outer join, cross join and natural
join:
SELECT *
FROM table1
INNER JOIN table2 ON conditions
Code language: SQL (Structured Query Language) (sql)
SELECT *
FROM table1
LEFT JOIN table2 ON conditions
Code language: SQL (Structured Query Language) (sql)
SELECT *
FROM table1
FULL OUTER JOIN table2 ON conditions
Code language: SQL (Structured Query Language) (sql)
SELECT *
FROM table1
CROSS JOIN table2;
Code language: SQL (Structured Query Language) (sql)
SELECT *
FROM table1
NATURAL JOIN table2;
Code language: SQL (Structured Query Language) (sql)
SELECT select_list
FROM table
ORDER BY column ASC [DESC], column2 ASC [DESC],...;
Code language: SQL (Structured Query Language) (sql)
SELECT *
FROM table
GROUP BY column_1, column_2, ...;
Code language: SQL (Structured Query Language) (sql)
SELECT *
FROM table
GROUP BY column_1
HAVING condition;
Code language: SQL (Structured Query Language) (sql)
Set operations
Combine the result set of two or more queries with UNION operator:
Modifying data
UPDATE table_name
SET column_1 = value_1,
...;
Code language: SQL (Structured Query Language) (sql)
Update data for a set of rows specified by a condition in the WHERE clause.
UPDATE table
SET column_1 = value_1,
...
WHERE condition;
Code language: SQL (Structured Query Language) (sql)
Performance
EXPLAIN query;
Code language: SQL (Structured Query Language) (sql)
Collect statistics:
ANALYZE table_name;
Code language: SQL (Structured Query Language) (sql)
PostgreSQL Fundamentals
SELECT
Column Aliases
ORDER BY
SELECT DISTINCT
WHERE
LIMIT
FETCH
IN
BETWEEN
LIKE
IS NULL
Table Aliases
Joins
INNER JOIN
LEFT JOIN
RIGHT JOIN
SELF-JOIN
FULL OUTER JOIN
Cross Join
Natural Join
GROUP BY
UNION
INTERSECT
HAVING
GROUPING SETS
CUBE
ROLLUP
Subquery
ANY
ALL
EXISTS
INSERT
INSERT Multiple Rows
UPDATE
UPDATE Join
DELETE
DELETE Join
Upsert
Managing Tables
Database Constraints
Primary Key
Foreign Key
CHECK Constraint
UNIQUE Constraint
NOT NULL Constraint
Boolean
CHAR, VARCHAR, and TEXT
NUMERIC
Integer
SERIAL
DATE
TIMESTAMP
Interval
TIME
UUID
JSON
HSTORE
Array
User-defined Data Types
CASE
COALESCE
NULLIF
CAST