Forked from DominikSerafin/01PostgreSQLSnippets.md
Last active
August 13, 2020 17:15
-
-
Save sblack4/b950a7d6f71bc423bbb5f3961b4e6db3 to your computer and use it in GitHub Desktop.
Revisions
-
sblack4 revised this gist
Aug 13, 2020 . 1 changed file with 23 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ # Describe current schema ### db info schema ``` SET search_path TO 'information_schema'; ``` ### list tables ``` -- list the tables in the schema SELECT table_name from information_schema.tables WHERE table_schema = 'myschema'; ``` ### describe table ``` SELECT column_name, is_nullable, data_type, character_maximum_length FROM information_schema.columns WHERE table_name = 'mytable' ORDER BY ordinal_position ``` -
DominikSerafin revised this gist
Nov 19, 2017 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,6 +27,9 @@ **Import Database #2** `psql -h localhost -U username -d dbname -f file.sql` **Import Database (from custom format, atomic)** `pg_restore -U username -d dbname -1 filename` **Dump Database (export)** `pg_dump -h localhost -U username dbname > /srv/db_name.backup` -
DominikSerafin revised this gist
Nov 19, 2017 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,3 +29,9 @@ **Dump Database (export)** `pg_dump -h localhost -U username dbname > /srv/db_name.backup` **Dump Database (custom compressed format)** `pg_dump -Fc -h localhost -U username dbname > /srv/db_name.backup` **Dump Database (one line with password)** `PGPASSWORD=abc123 pg_dump -h localhost -U username dbname > /srv/db_name.backup` -
DominikSerafin revised this gist
Apr 5, 2017 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,10 @@ # Database Export and Import 1. `pg_dump -h localhost -U username dbname -w > "/backups/dbname-$(date +'%y-%m-%d-%H%M').backup"` 2. `su - postgres` 3. `psql --set ON_ERROR_STOP=on --single-transaction db_name < backup-file` 4. grant all permissions to the db for the db user - `GRANT ALL ON ALL TABLES IN SCHEMA public to username; ` - `GRANT ALL ON ALL SEQUENCES IN SCHEMA public to username; ` - `GRANT ALL ON ALL FUNCTIONS IN SCHEMA public to username; ` -
DominikSerafin revised this gist
Apr 5, 2017 . 2 changed files with 14 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,25 +7,25 @@ `su - postgres` **Create database user** `createuser -P username` **Delete database user** `dropuser username` **Create database** `createdb dbname` **Drop database** `dropdb dbname` **Execute PSQL Command** `psql -c <somecommand>` **Import Database** `psql -h localhost -U username db_name < /srv/db_name.backup` **Import Database #2** `psql -h localhost -U username -d dbname -f file.sql` **Dump Database (export)** `pg_dump -h localhost -U username dbname > /srv/db_name.backup` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ `\du` or `\dg` **Select database** `\c db` or `\connect dbname` **See currently selected database** `\c` or `\connect` @@ -25,19 +25,19 @@ `\q` or `CTRL+D` **Create user** `CREATE USER username WITH PASSWORD 'password';` **Drop User** `DROP USER username;` **Create Database with owner** `CREATE DATABASE dbname WITH OWNER username;` **Change user password** `ALTER USER postgres PASSWORD 'new_password';` **Give privileges to database to user** `GRANT ALL PRIVILEGES ON DATABASE dbname TO username;` **Drop Database** `DROP DATABASE dbname;` -
DominikSerafin revised this gist
Apr 5, 2017 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -25,7 +25,10 @@ `\q` or `CTRL+D` **Create user** `CREATE USER username WITH password 'tmppassword';` **Drop User** `DROP USER username` **Create Database with owner** `CREATE DATABASE mydb WITH OWNER ramesh;` -
DominikSerafin revised this gist
Apr 5, 2017 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,5 +24,8 @@ **Import Database** `psql -U postgres -h localhost db_name < /srv/db_name.backup` **Import Database #2** `psql -h hostname -d databasename -U username -f file.sql` **Dump Database (export)** `pg_dump -U postgres -h localhost db_name > /srv/db_name.backup` -
DominikSerafin revised this gist
Apr 2, 2017 . 2 changed files with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,7 +21,7 @@ **Execute PSQL Command** `psql -c <somecommand>` **Import Database** `psql -U postgres -h localhost db_name < /srv/db_name.backup` **Dump Database (export)** This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,6 +12,9 @@ **List users** `\du` or `\dg` **Select database** `\c db` or `\connect db` **See currently selected database** `\c` or `\connect` -
DominikSerafin revised this gist
Jan 15, 2017 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,4 +5,5 @@ # Resources * [PostgreSQL command line cheatsheet](https://gist.github.com/Kartones/dd3ff5ec5ea238d4c546) * [Hyperpolyglot Cheatsheet](http://hyperpolyglot.org/db) -
DominikSerafin revised this gist
Jan 7, 2017 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,8 @@ # Notes * 'postgres' user is superuser # Resources * [Hyperpolyglot Cheatsheet](http://hyperpolyglot.org/db) -
DominikSerafin revised this gist
Jan 7, 2017 . 2 changed files with 12 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,6 +18,9 @@ **Drop database** `dropdb db_name` **Execute PSQL Command** `psql -c <somecommand>` **Dump Database** `psql -U postgres -h localhost db_name < /srv/db_name.backup` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,19 @@ # Queries and commands inside PSQL **General Help** `\?` **Syntax Help on SQL Commands** `\help <somequery>` **List Databases** `\l` or `\list` **List users** `\du` or `\dg` **See currently selected database** `\c` or `\connect` **Import database** `\i <file>` -
DominikSerafin revised this gist
Jan 7, 2017 . 1 changed file with 17 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,20 @@ # Queries and commands inside PSQL **List Databases** `\l` **List users** `\du` **See currently selected database** `\c` **Import database** `\i <file>` **Quit PSQL** `\q` or `CTRL+D` **Create user** `CREATE USER ramesh WITH password 'tmppassword';` @@ -12,14 +27,5 @@ **Give privileges to database to user** `GRANT ALL PRIVILEGES ON DATABASE db TO user;` **Drop Database** `DROP DATABASE db;` -
DominikSerafin revised this gist
Jan 7, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ # Bash Commands **Login into psql** `sudo -u postgres psql` **Login into postgres user** -
DominikSerafin revised this gist
Jan 7, 2017 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,25 +1,25 @@ # Bash Commands **Log in to psql** `sudo -u postgres psql` **Login into postgres user** `su - postgres` **Create database user** `createuser -P yournamehere` **Delete database user** `dropuser` **Create database** `createdb db_name` **Drop database** `dropdb db_name` **Dump Database** `psql -U postgres -h localhost db_name < /srv/db_name.backup` **Dump Database (export)** `pg_dump -U postgres -h localhost db_name > /srv/db_name.backup` -
DominikSerafin renamed this gist
Jan 7, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
DominikSerafin revised this gist
Jan 7, 2017 . 2 changed files with 10 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,23 +1,25 @@ # Bash Commands **Log in to psql** - `sudo -u postgres psql` **Login into postgres user** - `su - postgres` **Create database user** - `createuser -P yournamehere` **Delete database user** - `dropuser` **Create database** - `createdb db_name` **Drop database** - `dropdb db_name` **Dump Database** - `psql -U postgres -h localhost db_name < /srv/db_name.backup` **Dump Database (export)** - `pg_dump -U postgres -h localhost db_name > /srv/db_name.backup` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,33 +1,25 @@ # Queries and commands inside PSQL **Create user** `CREATE USER ramesh WITH password 'tmppassword';` **Create Database with owner** `CREATE DATABASE mydb WITH OWNER ramesh;` **Change user password** `ALTER USER postgres PASSWORD 'newPassword';` **Give privileges to database to user** `GRANT ALL PRIVILEGES ON DATABASE db TO user;` **List Databases** `\l` **List users** `\du` **Import database** `\i <file>` **Quit PSQL** `\q` or `CTRL+D` -
DominikSerafin revised this gist
Jan 7, 2017 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,25 +1,33 @@ # Queries and commands inside PSQL **Create user** `CREATE USER ramesh WITH password 'tmppassword';` **Create Database with owner** `CREATE DATABASE mydb WITH OWNER ramesh;` **Change user password** `ALTER USER postgres PASSWORD 'newPassword';` **Give privileges to database to user** `GRANT ALL PRIVILEGES ON DATABASE db TO user;` **List Databases** `\l` **List users** `\du` **Import database** `\i <file>` **Quit PSQL** `\q` or `CTRL+D` -
DominikSerafin revised this gist
Jan 7, 2017 . 3 changed files with 6 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,2 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ # Notes * 'postgres' user is superuser This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,6 @@ # Database Export and Import 1. `pg_dump -U db_user -h localhost db_name -w > "/srv/dbbackups/db_name-$(date +'%y-%m-%d-%H%M').backup"` 2. `su - postgres` 3. `psql --set ON_ERROR_STOP=on --single-transaction db_name < backup-file` -
DominikSerafin revised this gist
Jan 7, 2017 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ # Queries and commands inside PSQL **Create user** `CREATE USER ramesh WITH password 'tmppassword';` -
DominikSerafin revised this gist
Jan 7, 2017 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,13 @@ **Create user** `CREATE USER ramesh WITH password 'tmppassword';` **Create Database with owner** `CREATE DATABASE mydb WITH OWNER ramesh;` **Change user password** `ALTER USER postgres PASSWORD 'newPassword';` **Give privileges to database to user** `GRANT ALL PRIVILEGES ON DATABASE db TO user;` **List Databases** -
DominikSerafin renamed this gist
Jan 7, 2017 . 1 changed file with 9 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,23 +1,23 @@ # Create user `CREATE USER ramesh WITH password 'tmppassword';` # Create Database with owner `CREATE DATABASE mydb WITH OWNER ramesh;` # Change user password `ALTER USER postgres PASSWORD 'newPassword';` # Give privileges to database to user `GRANT ALL PRIVILEGES ON DATABASE db TO user;` **List Databases** `\l` **List users** `\du` **Import database** `\i <file>` **Quit PSQL** `\q` or `CTRL+D` -
DominikSerafin renamed this gist
Jan 7, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
DominikSerafin revised this gist
Jan 7, 2017 . 1 changed file with 10 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,20 +1,23 @@ ### Create user `CREATE USER ramesh WITH password 'tmppassword';` ### Create Database with owner `CREATE DATABASE mydb WITH OWNER ramesh;` ### Change user password `ALTER USER postgres PASSWORD 'newPassword';` ### Give privileges to database to user `GRANT ALL PRIVILEGES ON DATABASE db TO user;` ### List Databases `\l` ### List users `\du` ### Import database `\i <file>` ### Quit PSQL `\q` or CTRL+D -
DominikSerafin revised this gist
Dec 26, 2016 . 2 changed files with 15 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,21 @@ ### Log in to psql - `sudo -u postgres psql` ### Login into postgres user - `su - postgres` ### Create database user - `createuser -P yournamehere` ### Delete database user - `dropuser` ### Create database - `createdb db_name` ### Drop database - `dropdb db_name` ### Dump Database - `psql -U postgres -h localhost db_name < /srv/db_name.backup` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,19 +18,3 @@ ### Import database - `\i <file>` -
DominikSerafin revised this gist
Dec 26, 2016 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,6 @@ ### Log in to psql - `sudo -u postgres psql` ### Dump Database - `psql -U postgres -h localhost db_name < /srv/db_name.backup` -
DominikSerafin revised this gist
Dec 26, 2016 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +0,0 @@ -
DominikSerafin revised this gist
Dec 26, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ ### Notes - 'postgres' user is superuser -
DominikSerafin revised this gist
Dec 26, 2016 . No changes.There are no files selected for viewing
-
DominikSerafin revised this gist
Dec 26, 2016 . 4 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.File renamed without changes.File renamed without changes. -
DominikSerafin revised this gist
Dec 26, 2016 . 2 changed files with 3 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ - 'postgres' user is superuser This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,5 @@ ### Dump Database - `psql -U postgres -h localhost db_name < /srv/db_name.backup` ### Dump Database (export) - `pg_dump -U postgres -h localhost db_name > /srv/db_name.backup`
NewerOlder