{"id":14973877,"url":"https://github.com/andrewkslv/postgresql-cheat-sheet","last_synced_at":"2025-06-18T06:33:33.556Z","repository":{"id":87077709,"uuid":"121983862","full_name":"andrewkslv/postgresql-cheat-sheet","owner":"andrewkslv","description":"PostgreSQL (psql) commands in one page for the SQL REPL","archived":false,"fork":false,"pushed_at":"2021-06-10T19:06:52.000Z","size":16,"stargazers_count":12,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-11T21:30:35.065Z","etag":null,"topics":["postgres","postgresql","psql","psql-cheatsheet"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andrewkslv.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-18T19:38:00.000Z","updated_at":"2024-09-10T00:09:00.000Z","dependencies_parsed_at":null,"dependency_job_id":"4dd24f10-e32c-4fb7-9759-0985cb72a410","html_url":"https://github.com/andrewkslv/postgresql-cheat-sheet","commit_stats":{"total_commits":22,"total_committers":2,"mean_commits":11.0,"dds":0.2272727272727273,"last_synced_commit":"0047e146dd2c8d7a78eedb5c4dd84b8eb97c65cb"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/andrewkslv/postgresql-cheat-sheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewkslv%2Fpostgresql-cheat-sheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewkslv%2Fpostgresql-cheat-sheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewkslv%2Fpostgresql-cheat-sheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewkslv%2Fpostgresql-cheat-sheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andrewkslv","download_url":"https://codeload.github.com/andrewkslv/postgresql-cheat-sheet/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewkslv%2Fpostgresql-cheat-sheet/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260506241,"owners_count":23019410,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["postgres","postgresql","psql","psql-cheatsheet"],"created_at":"2024-09-24T13:49:37.381Z","updated_at":"2025-06-18T06:33:28.543Z","avatar_url":"https://github.com/andrewkslv.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# PostgreSQL Cheat Sheet\nPostgreSQL (psql) commands in one page for the SQL REPL from [postgresql.org](https://www.postgresql.org/docs/10/static/index.html).\n\n\u003cimg src=\"https://i.imgur.com/nueSCcH.gif\" width=\"250\"\u003e\n\n## Basics\n| Name | Description |\n|------|-------------|\n| `psql` | Connect to the database. |\n| `psql -d \u003cdb\u003e -U \u003cuser\u003e` | Connect to localhost `db` as `user` name. |\n| `psql create database \u003cdb\u003e` | Create database with `db` name. |\n| `create role \u003cpg_user\u003e with LOGIN ENCRYPTED PASSWORD \u003cpassword\u003e` | Create a user `pg_user` with `password`. |\n| `create database \u003cdb\u003e owner \u003cuser\u003e;` | Create a database with name `db` which belongs to `user`.\n| `create extension if not exists \"uuid-ossp\";` | Create useful extension with functions like `uuid_generate_v4`. [full doc](https://www.postgresql.org/docs/current/static/uuid-ossp.html)\n| `create extension if not exists \"hstore\";` | Create useful extension [hstore](https://www.postgresql.org/docs/current/static/hstore.html).\n| `revoke all on database \u003cdb\u003e from public;` |\n| `grant connect on database \u003cdb\u003e to \u003cuser\u003e;` |\n| `grant all on database \u003cdb\u003e to \u003cuser\u003e;` |\n| `alter role \u003cuser\u003e set search_path = \u003cschemaname\u003e;` |\n\n\n## General Purpose\n| Name | Description |\n|------|-------------|\n| `\\q` | Quit from psql. |\n| `\\?` | All commands of psql. |\n| `\\l+` | Lists all the databases with size. |\n| `\\c db_name` | Connect to database `db_name`. |\n| `\\dn` | List all schemas. |\n| `\\dt schema_name.*` | List all tables in `schema_name`. |\n| `\\d table_name` | List columns on table. |\n| `\\df` | List functions |\n| `\\dv` | List views. |\n| `\\x auto` | Super pretty query output for long rows. |\n| `\\t` | Turn off/on output header and row count. |\n| `\\h` | Help menu. |\n| `\\e` | Open an editor defined by your EDITOR environment variable, and put the most recent command entered in psql into the buffer. If it's vim editor use `shift + :` and tap `q` to quite or `wq` to resolve query. More vim [commands](https://vim.rtorr.com/). |\n| `\\ef [function]` | Open an editor, and put the function into the buffer. Without a function, it provides a convenient template for creating a new function. |\n| `\\s` | Print out your psql history to STDOUT. |\n| `\\timing` | Show timing for query execution. |\n\n\nHope you would be cool and efficient with this little cheat sheet.\n\nHelp me to fill out the rest of these useful commands if you think that might be helpful for you.\n\n## Config\n\n```\n$ cat ~/.psqlrc\n\\set ECHO none\n\\set PROMPT1 '%n@%/%R%# %x '\n\\x auto\n\\set ON_ERROR_STOP on\n\\set ON_ERROR_ROLLBACK interactive\n\n\\pset null '¤'\n\\pset linestyle 'unicode'\n\n\\pset unicode_border_linestyle single\n\\pset unicode_column_linestyle single\n\\pset unicode_header_linestyle double\n\nset intervalstyle to 'postgres_verbose';\n\n\\setenv LESS '-iMFXSx4R'\n\\set ECHO all\n```\n\n## Generating and inserting fake data\n\n```psql\nCREATE TABLE some_table (id bigserial PRIMARY KEY, a float);​\nSELECT create_distributed_table('some_table', 'id');​​​\n\nINSERT INTO some_table (a) SELECT random() * 100000 FROM generate_series(1, 1000000) i;    -- 2 secs  (40MB)​\nINSERT INTO some_table (a) SELECT random() * 100000 FROM generate_series(1, 10000000) i;   -- 20 secs (400MB)​\nINSERT INTO some_table (a) SELECT random() * 100000 FROM generate_series(1, 100000000) i;  -- 300 secs (4GB)​\nINSERT INTO some_table (a) SELECT random() * 100000 FROM generate_series(1, 1000000000) i; -- 40 mins (40GB)​\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrewkslv%2Fpostgresql-cheat-sheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandrewkslv%2Fpostgresql-cheat-sheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrewkslv%2Fpostgresql-cheat-sheet/lists"}