sudo service postgresql stop
sudo service postgresql start
sudo service postgresql restart
sudo systemctl restart postgresql
There are many CREATE
choices, like CREATE DATABASE __database_name__
, CREATE TABLE __table_name__
...
Parameters differ but can be checked at the official documentation.
SELECT datname, application_name, pid, backend_start, query_start, state_change, state, query
FROM pg_stat_activity
WHERE datname='__database_name__';
SELECT
t.relname AS table_name,
i.relname AS index_name,
a.attname AS column_name
FROM
pg_class t,
pg_class i,
pg_index ix,
pg_attribute a,
pg_namespace n
WHERE
t.oid = ix.indrelid
AND i.oid = ix.indexrelid
AND a.attrelid = t.oid
AND a.attnum = ANY(ix.indkey)
AND t.relnamespace = n.oid
AND n.nspname = 'kartones'
ORDER BY
t.relname,
i.relname
SELECT * FROM pg_stat_activity WHERE waiting='t'
SELECT
pg_stat_get_backend_pid(s.backendid) AS procpid,
pg_stat_get_backend_activity(s.backendid) AS current_query
FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
SELECT datname, numbackends FROM pg_stat_database;
INSERT INTO some_table (a_float_value) SELECT random() * 100000 FROM generate_series(1, 1000000) i;
sudo su - postgres
psql
GRANT ALL PRIVILEGES ON DATABASE TO ;
GRANT USAGE ON SCHEMA public TO ;
GRANT CONNECT ON DATABASE TO ;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO ;
GRANT SELECT, UPDATE, INSERT ON ALL TABLES IN SCHEMA public TO ;
GRANT SELECT, UPDATE, INSERT ON <table_name> TO ;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO ;
SELECT * FROM <table_name>;
SELECT * FROM <table_name> LIMIT 1;
SELECT * FROM <table_name> WHERE <column_name> = <value>;
INSERT INTO <table_name> VALUES( <value_1>, <value_2> );
UPDATE <table_name>
SET <column_1> = <value_1>, <column_2> = <value_2>
WHERE <column_1> = <value>;
DELETE FROM <table_name>;
DELETE FROM <table_name>
WHERE <column_name> = <value>;
EXPLAIN query;
EXPLAIN ANALYZE query;
ANALYZE table_name;
Some interesting flags (to see all, use -h
or --help
depending on your psql version)
Most \d commands support additional param of __schema__.name__
and accept wildcards like *.*
SELECT current_database() as database,
pg_size_pretty(total_database_size) as total_database_size,
schema_name,
table_name,
pg_size_pretty(total_table_size) as total_table_size,
pg_size_pretty(table_size) as table_size,
pg_size_pretty(index_size) as index_size
FROM ( SELECT table_name,
table_schema as schema_name,
pg_database_size(current_database()) as total_database_size,
pg_total_relation_size(table_name) as total_table_size,
pg_relation_size(table_name) as table_size,
pg_indexes_size(table_name) as index_size
FROM information_schema.tables
WHERE table_schema=current_schema() and table_name like 'table_%'
ORDER BY total_table_size
) as sizes;
COPY table_name [ ( column_name [, ...] ) ]
FROM { 'filename' | STDIN }
[ [ WITH ] ( option [, ...] ) ]
COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }
TO { 'filename' | STDOUT }
[ [ WITH ] ( option [, ...] ) ]
SELECT table_catalog, table_schema, table_name, privilege_type
FROM information_schema.table_privileges
WHERE grantee = 'user_to_check' ORDER BY table_name;
SELECT
r.rolname,
r.rolsuper,
r.rolinherit,
r.rolcreaterole,
r.rolcreatedb,
r.rolcanlogin,
r.rolconnlimit,
r.rolvaliduntil,
ARRAY(SELECT b.rolname
FROM pg_catalog.pg_auth_members m
JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
WHERE m.member = r.oid) as memberof,
r.rolreplication
FROM pg_catalog.pg_roles r
ORDER BY 1;
SELECT grantee, privilege_type
FROM information_schema.role_table_grants
WHERE table_name='name-of-the-table';
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE datname = current_database() AND pid <> pg_backend_pid();
ALTER TABLE <table_name> IF EXISTS
ADD <column_name> <data_type> [<constraints>];
ALTER TABLE <table_name> IF EXISTS
ALTER <column_name> TYPE <data_type> [<constraints>];
ALTER TABLE <table_name> IF EXISTS
DROP <column_name>;
ALTER TABLE <table_name>
ADD COLUMN <column_name> SERIAL PRIMARY KEY;
INSERT INTO <table_name>
VALUES (DEFAULT, <value1>);
INSERT INTO <table_name> (<column1_name>,<column2_name>)
VALUES ( <value1>,<value2> );
…
A PostgreSQL commands cheat sheet is a quick reference guide that lists essential commands used in PostgreSQL (often referred to as Postgres), an open-source relational database management system. These commands are used for various tasks like creating databases, tables, managing users, performing data operations, and more.
General Commands
SQL Commands
User and Permission Commands
Please note that these are only basic commands. PostgreSQL offers rich features and commands for more complex operations and queries.
PostgreSQL, often simply “Postgres”, is an open-source object-relational database management system (ORDBMS) emphasising extensibility and SQL compliance.
It was created at the University of California, Berkeley, in the 1980s and has since become one of the most advanced open-source databases available.
Here are some key features of Postgres:
1.SQL Compliance: It’s fully ACID-compliant (Atomicity, Consistency, Isolation, Durability), supporting foreign keys, joins, views, triggers, and stored procedures in multiple programming languages.
2. Object-Relational: It is not just a relational database; it also includes object-oriented database features. You can, for instance, define your data types.
3. MVCC (Multi-Version Concurrency Control): This feature allows multiple transactions to access the same data simultaneously without interfering with each other, improving the performance of the database in multi-user environments.
4. Extensibility: PostgreSQL is highly extensible. It allows for custom functions, operators, and aggregate functions. It also supports various procedural languages, like PL/pgSQL (similar to Oracle’s PL/SQL), JavaScript, Python, and more.
5. Replication and High Availability: Postgres supports various types of replication (including master-slave and multi-master) to enhance read scaling and ensure data durability and high availability.
6. Full Text Search: It has built-in support for full-text search, searching a database for rows that match a given text query.
7. Security: Postgres includes robust security features like a solid access-control system, views and granular permissions, and built-in SSL support.
8. Large community: Since it’s open source, it has a large community of developers who constantly contribute to its development, providing regular updates, new features, and quick bug fixes.
Programming can be fun, so can cryptography; however they should not be combined.
Kreitzberg and Shneiderman
…
…