You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
\? - Help
\? <keyword> - Help for SQL keyword
\du - List roles
\l - List databases
\c <db_name> - Connect to database
\dt - List tables (with selected DB)
\d <table_name> - List columns in table
\d+ <table_name> - Describe table
\q - Exiting from console
Dumping postgres database
pg_dump db_name > db_name_dump.sql # Dump database_name
pg_dumpall > full_dump.sql # Dump all databases # Example on dumping & restoring postgresql database# Don't forget to 'CREATE DATABASE newdb;'
pg_dump -Ftar -fdb_name.tar db_name
pg_restore -d newdb db_name_dump.sql
Running a SQL script (command line)
psql -f script.sql db_name
Basic SQL
-- Get all columns and rows from a tableSELECT*FROM table_name;
-- Add a new rowINSERT INTO table_name (column1,column2) VALUES (1, 'one');
-- Update a rowUPDATE table_name SET foo ='bar'WHERE id =1;
-- Delete a rowDELETEFROM table_name WHERE id =1;
Some usefull sql commands syntax
-- Comments-- This is a comment to end of line/* C-like comment, possiblymultiple lines */-- Changing password for postgresql user *username* ALTERUSER username WITH PASSWORD 'username';
-- Explain query
EXPLAIN ANALYZE SELECT"products".*FROM"products"WHERE (lower(name) like'%pre%'orlower(description) like'%pre%')
ORDER BY name ASCLIMIT100 OFFSET 100;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------Limit (cost=29995.24..29995.49 rows=100 width=2190) (actual time=1017.821..1017.832 rows=100 loops=1)
-> Sort (cost=29994.99..30126.81 rows=52728 width=2190) (actual time=1017.809..1017.819 rows=200 loops=1)
Sort Key: name
Sort Method: top-N heapsort Memory: 78kB
-> Seq Scan on products (cost=0.00..27716.12 rows=52728 width=2190) (actual time=0.955..973.433 rows=20569 loops=1)
Filter: ((lower((name)::text) ~~ '%pre%'::text) OR (lower(description) ~~ '%pre%'::text))
Rows Removed by Filter: 651987
Total runtime: 1018.692 ms
(8 rows)
-- Create databaseCREATEDATABASEdb_name;
-- Create table with auto numbering integer idCREATETABLEtable_name (
id serialPRIMARY KEY,
name varchar(50) UNIQUE NOT NULL,
dateCreated timestamp DEFAULT current_timestamp
);
-- Add a primary keyALTERTABLE table_name ADD PRIMARY KEY (id);
-- Create an INDEXCREATEUNIQUE INDEXindex_nameON table_name (column_name1, column_name2, ...);
-- Search using a regular expressionSELECT column FROM table_name WHERE column ~ 'foo.*';
-- The first N recordsSELECT columns FROM table_name LIMIT10;
-- PaginationSELECT columns FROM table_name LIMIT10 OFFSET 30;
-- Prepared Statements
PREPARE preparedInsert (int, varchar) ASINSERT INTO tableName (intColumn, charColumn) VALUES ($1, $2);
EXECUTE preparedInsert (1,'a');
EXECUTE preparedInsert (2,'b');
DEALLOCATE preparedInsert;
-- Create a FunctionCREATE OR REPLACEFUNCTIONmonth (timestamp) RETURNS integerAS'SELECT date_part(''month'', $1)::integer;'
LANGUAGE 'sql';
-- Table Maintenance
VACUUM ANALYZE table_name;
-- Reindex a database, table or index
REINDEX DATABASE db_name;
-- Import from a file
COPY dest_table FROM'/tmp/somefile';
-- Show all runtime parameters
SHOW ALL;
-- Grant all permissions to a userGRANT ALL PRIVILEGES ON table_name TO user_name;
-- Perform a transactionBEGIN TRANSACTION
UPDATE accounts SET balance +=50WHERE id =1;
COMMIT;