psql -U username -d database_name
l
dt
d table_name
q
CREATE DATABASE database_name;
DROP DATABASE database_name;
ALTER DATABASE old_name RENAME TO new_name;
CREATE TABLE table_name (
column1 datatype1 constraints,
column2 datatype2 constraints,
...
);
Example:
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
hire_date DATE,
salary NUMERIC(10, 2)
);
-- Add a column
ALTER TABLE table_name ADD COLUMN column_name datatype;
-- Drop a column
ALTER TABLE table_name DROP COLUMN column_name;
-- Rename a column
ALTER TABLE table_name RENAME COLUMN old_name TO new_name;
-- Change column data type
ALTER TABLE table_name ALTER COLUMN column_name TYPE new_datatype;
DROP TABLE table_name;
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example:
INSERT INTO employees (first_name, last_name, email, hire_date, salary)
VALUES ('John', 'Doe', 'john.doe@example.com', '2023-01-15', 50000.00);
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE employees
SET salary = 55000.00
WHERE id = 1;
DELETE FROM table_name
WHERE condition;
Example:
DELETE FROM employees
WHERE id = 1;
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
SELECT first_name, last_name, salary
FROM employees
WHERE salary > 50000;
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
Example:
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC, last_name ASC;
SELECT column1, column2, ...
FROM table_name
LIMIT number_of_rows OFFSET start_row;
Example:
SELECT *
FROM employees
LIMIT 10 OFFSET 20;
SELECT DISTINCT column1, column2, ...
FROM table_name;
Example:
SELECT DISTINCT department
FROM employees;
SELECT column1, column2, ...
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
Example:
SELECT employees.first_name, employees.last_name, departments.name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
SELECT column1, column2, ...
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
SELECT column1, column2, ...
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;
SELECT column1, column2, ...
FROM table1
FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
Example:
SELECT COUNT(*) as total_employees
FROM employees;
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Example:
SELECT SUM(salary) as total_salary
FROM employees;
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SELECT MIN(column_name), MAX(column_name)
FROM table_name
WHERE condition;
SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1;
Example:
SELECT department, AVG(salary) as avg_salary
FROM employees
GROUP BY department;
SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1
HAVING condition;
Example:
SELECT department, AVG(salary) as avg_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;
CREATE INDEX index_name
ON table_name (column1, column2, ...);
Example:
CREATE INDEX idx_last_name
ON employees (last_name);
CREATE UNIQUE INDEX index_name
ON table_name (column1, column2, ...);
DROP INDEX index_name;
BEGIN;
COMMIT;
ROLLBACK;
Example:
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
CREATE VIEW high_salary_employees AS
SELECT *
FROM employees
WHERE salary > 75000;
DROP VIEW view_name;
CREATE FUNCTION function_name(parameter1 datatype, parameter2 datatype, ...)
RETURNS return_datatype AS $$
BEGIN
-- function body
RETURN result;
END;
$$ LANGUAGE plpgsql;
Example:
CREATE FUNCTION get_employee_salary(emp_id integer)
RETURNS numeric AS $$
DECLARE
emp_salary numeric;
BEGIN
SELECT salary INTO emp_salary
FROM employees
WHERE id = emp_id;
RETURN emp_salary;
END;
$$ LANGUAGE plpgsql;
SELECT function_name(argument1, argument2, ...);
Example:
SELECT get_employee_salary(1);
CREATE TRIGGER trigger_name
{BEFORE | AFTER} {INSERT | UPDATE | DELETE}
ON table_name
FOR EACH ROW
EXECUTE FUNCTION trigger_function();
Example:
CREATE OR REPLACE FUNCTION update_modified_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.modified = now();
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER update_customer_modtime
BEFORE UPDATE ON customers
FOR EACH ROW
EXECUTE FUNCTION update_modified_column();
CREATE USER username WITH PASSWORD 'password';
GRANT privilege_type ON table_name TO username;
Example:
GRANT SELECT, INSERT, UPDATE ON employees TO john_doe;
REVOKE privilege_type ON table_name FROM username;
pg_dump database_name > backup_file.sql
psql database_name < backup_file.sql
Use EXPLAIN to show the query plan:
EXPLAIN SELECT * FROM employees WHERE salary > 50000;
Update statistics used by the query planner:
ANALYZE table_name;
Clean up dead tuples and optionally analyze the database:
VACUUM (ANALYZE) table_name;
CREATE MATERIALIZED VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
REFRESH MATERIALIZED VIEW view_name;
2024 © All rights reserved - buraxta.com