Skip to content

Instantly share code, notes, and snippets.

@sblack4
Forked from DominikSerafin/01PostgreSQLSnippets.md
Last active August 13, 2020 17:15
Show Gist options
  • Select an option

  • Save sblack4/b950a7d6f71bc423bbb5f3961b4e6db3 to your computer and use it in GitHub Desktop.

Select an option

Save sblack4/b950a7d6f71bc423bbb5f3961b4e6db3 to your computer and use it in GitHub Desktop.

Revisions

  1. sblack4 revised this gist Aug 13, 2020. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions 05schema-exploring.md
    Original 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
    ```
  2. @DominikSerafin DominikSerafin revised this gist Nov 19, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions 03psql-pg_dump-etc.md
    Original 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`

  3. @DominikSerafin DominikSerafin revised this gist Nov 19, 2017. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions 03psql-pg_dump-etc.md
    Original 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`
  4. @DominikSerafin DominikSerafin revised this gist Apr 5, 2017. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions 02guides.md
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,10 @@

    # 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"`
    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 user; `
    - `GRANT ALL ON ALL SEQUENCES IN SCHEMA public to user; `
    - `GRANT ALL ON ALL FUNCTIONS IN SCHEMA public to 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; `
  5. @DominikSerafin DominikSerafin revised this gist Apr 5, 2017. 2 changed files with 14 additions and 14 deletions.
    14 changes: 7 additions & 7 deletions 03psql-pg_dump-etc.md
    Original file line number Diff line number Diff line change
    @@ -7,25 +7,25 @@
    `su - postgres`

    **Create database user**
    `createuser -P yournamehere`
    `createuser -P username`

    **Delete database user**
    `dropuser`
    `dropuser username`

    **Create database**
    `createdb db_name`
    `createdb dbname`

    **Drop database**
    `dropdb db_name`
    `dropdb dbname`

    **Execute PSQL Command**
    `psql -c <somecommand>`

    **Import Database**
    `psql -U postgres -h localhost db_name < /srv/db_name.backup`
    `psql -h localhost -U username db_name < /srv/db_name.backup`

    **Import Database #2**
    `psql -h hostname -d databasename -U username -f file.sql`
    `psql -h localhost -U username -d dbname -f file.sql`

    **Dump Database (export)**
    `pg_dump -U postgres -h localhost db_name > /srv/db_name.backup`
    `pg_dump -h localhost -U username dbname > /srv/db_name.backup`
    14 changes: 7 additions & 7 deletions 04queries.md
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@
    `\du` or `\dg`

    **Select database**
    `\c db` or `\connect db`
    `\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 'tmppassword';`
    `CREATE USER username WITH PASSWORD 'password';`

    **Drop User**
    `DROP USER username`
    `DROP USER username;`

    **Create Database with owner**
    `CREATE DATABASE mydb WITH OWNER ramesh;`
    `CREATE DATABASE dbname WITH OWNER username;`

    **Change user password**
    `ALTER USER postgres PASSWORD 'newPassword';`
    `ALTER USER postgres PASSWORD 'new_password';`

    **Give privileges to database to user**
    `GRANT ALL PRIVILEGES ON DATABASE db TO user;`
    `GRANT ALL PRIVILEGES ON DATABASE dbname TO username;`

    **Drop Database**
    `DROP DATABASE db;`
    `DROP DATABASE dbname;`
  6. @DominikSerafin DominikSerafin revised this gist Apr 5, 2017. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion 04queries.md
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,10 @@
    `\q` or `CTRL+D`

    **Create user**
    `CREATE USER ramesh WITH password 'tmppassword';`
    `CREATE USER username WITH password 'tmppassword';`

    **Drop User**
    `DROP USER username`

    **Create Database with owner**
    `CREATE DATABASE mydb WITH OWNER ramesh;`
  7. @DominikSerafin DominikSerafin revised this gist Apr 5, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions 03psql-pg_dump-etc.md
    Original 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`
  8. @DominikSerafin DominikSerafin revised this gist Apr 2, 2017. 2 changed files with 4 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion 03psql-pg_dump-etc.md
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@
    **Execute PSQL Command**
    `psql -c <somecommand>`

    **Dump Database**
    **Import Database**
    `psql -U postgres -h localhost db_name < /srv/db_name.backup`

    **Dump Database (export)**
    3 changes: 3 additions & 0 deletions 04queries.md
    Original 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`

  9. @DominikSerafin DominikSerafin revised this gist Jan 15, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion 01PostgreSQLSnippets.md
    Original file line number Diff line number Diff line change
    @@ -5,4 +5,5 @@

    # Resources

    * [Hyperpolyglot Cheatsheet](http://hyperpolyglot.org/db)
    * [PostgreSQL command line cheatsheet](https://gist.github.com/Kartones/dd3ff5ec5ea238d4c546)
    * [Hyperpolyglot Cheatsheet](http://hyperpolyglot.org/db)
  10. @DominikSerafin DominikSerafin revised this gist Jan 7, 2017. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions 01PostgreSQLSnippets.md
    Original 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)
  11. @DominikSerafin DominikSerafin revised this gist Jan 7, 2017. 2 changed files with 12 additions and 3 deletions.
    3 changes: 3 additions & 0 deletions 03psql-pg_dump-etc.md
    Original 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`

    12 changes: 9 additions & 3 deletions 04queries.md
    Original 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`
    `\l` or `\list`

    **List users**
    `\du`
    `\du` or `\dg`

    **See currently selected database**
    `\c`
    `\c` or `\connect`

    **Import database**
    `\i <file>`
  12. @DominikSerafin DominikSerafin revised this gist Jan 7, 2017. 1 changed file with 17 additions and 11 deletions.
    28 changes: 17 additions & 11 deletions 04queries.md
    Original 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;`

    **List Databases**
    `\l`

    **List users**
    `\du`

    **Import database**
    `\i <file>`

    **Quit PSQL**
    `\q` or `CTRL+D`
    **Drop Database**
    `DROP DATABASE db;`
  13. @DominikSerafin DominikSerafin revised this gist Jan 7, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 03psql-pg_dump-etc.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Bash Commands

    **Log in to psql**
    **Login into psql**
    `sudo -u postgres psql`

    **Login into postgres user**
  14. @DominikSerafin DominikSerafin revised this gist Jan 7, 2017. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions 03psql-pg_dump-etc.md
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,25 @@
    # Bash Commands

    **Log in to psql**
    - `sudo -u postgres psql`
    `sudo -u postgres psql`

    **Login into postgres user**
    - `su - postgres`
    `su - postgres`

    **Create database user**
    - `createuser -P yournamehere`
    `createuser -P yournamehere`

    **Delete database user**
    - `dropuser`
    `dropuser`

    **Create database**
    - `createdb db_name`
    `createdb db_name`

    **Drop database**
    - `dropdb db_name`
    `dropdb db_name`

    **Dump Database**
    - `psql -U postgres -h localhost db_name < /srv/db_name.backup`
    `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`
    `pg_dump -U postgres -h localhost db_name > /srv/db_name.backup`
  15. @DominikSerafin DominikSerafin renamed this gist Jan 7, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  16. @DominikSerafin DominikSerafin revised this gist Jan 7, 2017. 2 changed files with 10 additions and 16 deletions.
    18 changes: 10 additions & 8 deletions 03 psql-pg_dump-etc
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,25 @@
    ### Log in to psql
    # Bash Commands

    **Log in to psql**
    - `sudo -u postgres psql`

    ### Login into postgres user
    **Login into postgres user**
    - `su - postgres`

    ### Create database user
    **Create database user**
    - `createuser -P yournamehere`

    ### Delete database user
    **Delete database user**
    - `dropuser`

    ### Create database
    **Create database**
    - `createdb db_name`

    ### Drop database
    **Drop database**
    - `dropdb db_name`

    ### Dump Database
    **Dump Database**
    - `psql -U postgres -h localhost db_name < /srv/db_name.backup`

    ### Dump Database (export)
    **Dump Database (export)**
    - `pg_dump -U postgres -h localhost db_name > /srv/db_name.backup`
    8 changes: 0 additions & 8 deletions 04queries.md
    Original 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`
  17. @DominikSerafin DominikSerafin revised this gist Jan 7, 2017. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions 04queries.md
    Original 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`
  18. @DominikSerafin DominikSerafin revised this gist Jan 7, 2017. 3 changed files with 6 additions and 3 deletions.
    2 changes: 0 additions & 2 deletions 01 PostgreSQL Snippets
    Original file line number Diff line number Diff line change
    @@ -1,2 +0,0 @@
    ### Notes
    - 'postgres' user is superuser
    3 changes: 3 additions & 0 deletions 01PostgreSQLSnippets.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    # Notes

    * 'postgres' user is superuser
    4 changes: 3 additions & 1 deletion 02 guides → 02guides.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    ### Database Export and Import

    # 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`
  19. @DominikSerafin DominikSerafin revised this gist Jan 7, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions 04queries.md
    Original 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';`

  20. @DominikSerafin DominikSerafin revised this gist Jan 7, 2017. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions 04queries.md
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    # Create user
    **Create user**
    `CREATE USER ramesh WITH password 'tmppassword';`

    # Create Database with owner
    **Create Database with owner**
    `CREATE DATABASE mydb WITH OWNER ramesh;`

    # Change user password
    **Change user password**
    `ALTER USER postgres PASSWORD 'newPassword';`

    # Give privileges to database to user
    **Give privileges to database to user**
    `GRANT ALL PRIVILEGES ON DATABASE db TO user;`

    **List Databases**
  21. @DominikSerafin DominikSerafin renamed this gist Jan 7, 2017. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions queries.md → 04queries.md
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,23 @@
    ### Create user
    # Create user
    `CREATE USER ramesh WITH password 'tmppassword';`

    ### Create Database with owner
    # Create Database with owner
    `CREATE DATABASE mydb WITH OWNER ramesh;`

    ### Change user password
    # Change user password
    `ALTER USER postgres PASSWORD 'newPassword';`

    ### Give privileges to database to user
    # Give privileges to database to user
    `GRANT ALL PRIVILEGES ON DATABASE db TO user;`

    ### List Databases
    **List Databases**
    `\l`

    ### List users
    **List users**
    `\du`

    ### Import database
    **Import database**
    `\i <file>`

    ### Quit PSQL
    `\q` or CTRL+D
    **Quit PSQL**
    `\q` or `CTRL+D`
  22. @DominikSerafin DominikSerafin renamed this gist Jan 7, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  23. @DominikSerafin DominikSerafin revised this gist Jan 7, 2017. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions 04 queries
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,23 @@
    ### Create user
    - `CREATE USER ramesh WITH password 'tmppassword';`
    `CREATE USER ramesh WITH password 'tmppassword';`

    ### Create Database with owner
    - `CREATE DATABASE mydb WITH OWNER ramesh;`
    `CREATE DATABASE mydb WITH OWNER ramesh;`

    ### Change user password
    - `ALTER USER postgres PASSWORD 'newPassword';`
    `ALTER USER postgres PASSWORD 'newPassword';`

    ### Give privileges to database to user
    - `GRANT ALL PRIVILEGES ON DATABASE db TO user;`
    `GRANT ALL PRIVILEGES ON DATABASE db TO user;`

    ### List Databases
    - `\l`
    `\l`

    ### List users
    - `\du`
    `\du`

    ### Import database
    - `\i <file>`
    `\i <file>`

    ### Quit PSQL
    `\q` or CTRL+D
  24. @DominikSerafin DominikSerafin revised this gist Dec 26, 2016. 2 changed files with 15 additions and 16 deletions.
    15 changes: 15 additions & 0 deletions 03 psql-pg_dump-etc
    Original 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`

    16 changes: 0 additions & 16 deletions 04 queries
    Original file line number Diff line number Diff line change
    @@ -18,19 +18,3 @@

    ### Import database
    - `\i <file>`

    ### 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`

  25. @DominikSerafin DominikSerafin revised this gist Dec 26, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions 03 psql-pg_dump-etc
    Original 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`

  26. @DominikSerafin DominikSerafin revised this gist Dec 26, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion 01 notes
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    - 'postgres' user is superuser
  27. @DominikSerafin DominikSerafin revised this gist Dec 26, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions 01 PostgreSQL Snippets
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    ### Notes
    - 'postgres' user is superuser
  28. @DominikSerafin DominikSerafin revised this gist Dec 26, 2016. No changes.
  29. @DominikSerafin DominikSerafin revised this gist Dec 26, 2016. 4 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  30. @DominikSerafin DominikSerafin revised this gist Dec 26, 2016. 2 changed files with 3 additions and 6 deletions.
    1 change: 1 addition & 0 deletions notes
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    - 'postgres' user is superuser
    8 changes: 2 additions & 6 deletions psql-pg_dump-etc
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,5 @@
    ### Notes
    - 'postgres' user is superuser


    ### Dump Database
    - `psql -U postgres -h localhost enboard_dev < /srv/enboard_dev_db.backup`
    - `psql -U postgres -h localhost db_name < /srv/db_name.backup`

    ### Dump Database (export)
    - `pg_dump -U postgres -h localhost enboard_dev > /srv/enboard_dev_db.backup`
    - `pg_dump -U postgres -h localhost db_name > /srv/db_name.backup`