{
  "version": 1,
  "collections": [
    {
      "id": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "name": "psql",
      "color": "#4f8fd6"
    }
  ],
  "snippets": [
    {
      "id": "842899db-2834-5e98-adf5-f9cc828dfc33",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Connect to a database",
      "body": "psql -h {{host:localhost}} -p {{port:5432}} -U {{user:postgres}} -d {{database}}",
      "description": "",
      "tags": [
        "connect"
      ]
    },
    {
      "id": "cf34dbd8-90cf-56fe-8cd6-d51e701519e1",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Connect with a URL",
      "body": "psql \"postgresql://{{user}}@{{host}}:{{port:5432}}/{{database}}\"",
      "description": "",
      "tags": [
        "connect"
      ]
    },
    {
      "id": "77a0a91e-181a-5d3c-873b-132f1cf27eda",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Run one query and exit",
      "body": "psql -d {{database}} -c \"{{query}}\"",
      "description": "",
      "tags": [
        "connect"
      ]
    },
    {
      "id": "10ba61a2-208d-508b-b21a-475756bd95e9",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Run a SQL file",
      "body": "psql -d {{database}} -f {{file}}",
      "description": "",
      "tags": [
        "connect"
      ]
    },
    {
      "id": "3925a168-221d-584e-8a4f-65cae5568efa",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "List databases",
      "body": "\\l",
      "description": "",
      "tags": [
        "psql"
      ]
    },
    {
      "id": "1d2c4866-bb1e-55b2-81dc-6ddf351c4f84",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Connect to another database",
      "body": "\\c {{database}}",
      "description": "",
      "tags": [
        "psql"
      ]
    },
    {
      "id": "f18e71b1-927f-5fa4-8510-8b0750ffa3d6",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "List tables",
      "body": "\\dt",
      "description": "",
      "tags": [
        "psql"
      ]
    },
    {
      "id": "14d8a983-ed43-5506-b00c-40a1a6e8a59e",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "List tables in a schema",
      "body": "\\dt {{schema}}.*",
      "description": "",
      "tags": [
        "psql"
      ]
    },
    {
      "id": "ff988241-31e8-5810-a5b4-a6f82b16bfc2",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Describe a table",
      "body": "\\d+ {{table}}",
      "description": "",
      "tags": [
        "psql"
      ]
    },
    {
      "id": "9df183d3-b80d-5f41-a919-a1e676d4914f",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "List roles",
      "body": "\\du",
      "description": "",
      "tags": [
        "psql"
      ]
    },
    {
      "id": "1f0ad0d8-f534-53be-9d4a-668a842d1f41",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "List schemas",
      "body": "\\dn",
      "description": "",
      "tags": [
        "psql"
      ]
    },
    {
      "id": "197e2cb0-d39e-5f0d-a326-f9a0665a66f1",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Readable output for wide rows",
      "body": "\\x auto",
      "description": "",
      "tags": [
        "psql"
      ]
    },
    {
      "id": "1847bb15-c719-50f1-ad65-2ef0a43e609c",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Show how long each query takes",
      "body": "\\timing",
      "description": "",
      "tags": [
        "psql"
      ]
    },
    {
      "id": "164a5d47-8c93-55b5-a938-fb0f83d2259e",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Edit the query in your editor",
      "body": "\\e",
      "description": "",
      "tags": [
        "psql"
      ]
    },
    {
      "id": "dd4dcbaa-716f-5a22-8f69-c0ccbe425be2",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Export a query to CSV",
      "body": "\\copy ({{query}}) TO '{{file:output.csv}}' WITH CSV HEADER",
      "description": "Writes the file on your computer, not the server.",
      "tags": [
        "psql"
      ]
    },
    {
      "id": "f79a6e60-09b4-5789-a906-7aa393c0f96a",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Biggest tables",
      "body": "SELECT relname AS table, pg_size_pretty(pg_total_relation_size(relid)) AS size\nFROM pg_catalog.pg_statio_user_tables\nORDER BY pg_total_relation_size(relid) DESC\nLIMIT {{limit:20}};",
      "description": "Includes indexes and TOAST data.",
      "tags": [
        "sizes"
      ]
    },
    {
      "id": "d3778403-592f-526f-a00c-750986540021",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Size of a database",
      "body": "SELECT pg_size_pretty(pg_database_size('{{database}}'));",
      "description": "",
      "tags": [
        "sizes"
      ]
    },
    {
      "id": "06a9e297-32cc-50a8-a10d-77fc7da1b548",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Fast row count estimate",
      "body": "SELECT reltuples::bigint AS estimate FROM pg_class WHERE relname = '{{table}}';",
      "description": "Instant on huge tables, unlike count(*). Accurate as of the last ANALYZE.",
      "tags": [
        "sizes"
      ]
    },
    {
      "id": "52828d40-58b7-5161-bad9-162016fc7c10",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Queries running right now",
      "body": "SELECT pid, now() - query_start AS duration, state, query\nFROM pg_stat_activity\nWHERE state <> 'idle'\nORDER BY duration DESC;",
      "description": "",
      "tags": [
        "performance"
      ]
    },
    {
      "id": "85934c78-d0a2-5bb1-a431-ab7c500623ab",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Queries waiting on a lock",
      "body": "SELECT pid, pg_blocking_pids(pid) AS blocked_by, query\nFROM pg_stat_activity\nWHERE cardinality(pg_blocking_pids(pid)) > 0;",
      "description": "",
      "tags": [
        "performance"
      ]
    },
    {
      "id": "83df7416-026d-554e-bc88-061e813a0924",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Cancel a query",
      "body": "SELECT pg_cancel_backend({{pid}});",
      "description": "",
      "tags": [
        "performance"
      ]
    },
    {
      "id": "aa6b686c-0986-5437-8e3c-d47b20a826b8",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "End a connection",
      "body": "SELECT pg_terminate_backend({{pid}});",
      "description": "",
      "tags": [
        "performance"
      ]
    },
    {
      "id": "a3bedcd1-33b8-5497-a85a-47839a0ddd1a",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Explain a query with real timings",
      "body": "EXPLAIN (ANALYZE, BUFFERS) {{query}};",
      "description": "Actually runs the query. Wrap writes in BEGIN … ROLLBACK.",
      "tags": [
        "performance"
      ]
    },
    {
      "id": "b9279d71-345f-5a61-ab67-c104ed58b20b",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Indexes that are never used",
      "body": "SELECT relname AS table, indexrelname AS index, pg_size_pretty(pg_relation_size(indexrelid)) AS size\nFROM pg_stat_user_indexes\nWHERE idx_scan = 0\nORDER BY pg_relation_size(indexrelid) DESC;",
      "description": "",
      "tags": [
        "performance"
      ]
    },
    {
      "id": "9350ef86-313a-56dc-b247-b7a0d5030080",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Add an index without locking writes",
      "body": "CREATE INDEX CONCURRENTLY {{index_name}} ON {{table}} ({{column}});",
      "description": "",
      "tags": [
        "performance"
      ]
    },
    {
      "id": "1e6ee5f6-3e6f-5fc4-ac74-1c0a2709a090",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Columns of a table",
      "body": "SELECT column_name, data_type, is_nullable\nFROM information_schema.columns\nWHERE table_name = '{{table}}'\nORDER BY ordinal_position;",
      "description": "",
      "tags": [
        "queries"
      ]
    },
    {
      "id": "b1b38f87-f8d9-5437-97f0-8aec2d7ca2f9",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Find duplicate values",
      "body": "SELECT {{column}}, count(*)\nFROM {{table}}\nGROUP BY {{column}}\nHAVING count(*) > 1\nORDER BY count(*) DESC;",
      "description": "",
      "tags": [
        "queries"
      ]
    },
    {
      "id": "8d72c903-a627-5574-9a09-5d3b20c69b3f",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Create a user",
      "body": "CREATE ROLE {{user}} WITH LOGIN PASSWORD '{{password}}';",
      "description": "",
      "tags": [
        "roles"
      ]
    },
    {
      "id": "b9044f23-5782-54fb-9cdf-3c4959605442",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Give a user read-only access",
      "body": "GRANT USAGE ON SCHEMA {{schema:public}} TO {{user}};\nGRANT SELECT ON ALL TABLES IN SCHEMA {{schema:public}} TO {{user}};\nALTER DEFAULT PRIVILEGES IN SCHEMA {{schema:public}} GRANT SELECT ON TABLES TO {{user}};",
      "description": "The last line covers tables created later.",
      "tags": [
        "roles"
      ]
    },
    {
      "id": "0c874b16-d625-556a-b3a7-0b11a7bcbd8e",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Back up a database",
      "body": "pg_dump -Fc -d {{database}} -f {{file:backup.dump}}",
      "description": "Custom format: compressed, and lets pg_restore pick what to restore.",
      "tags": [
        "backup"
      ]
    },
    {
      "id": "dd267b22-4224-5916-ac17-5c505e5b8996",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Restore a backup",
      "body": "pg_restore --no-owner -d {{database}} {{file:backup.dump}}",
      "description": "",
      "tags": [
        "backup"
      ]
    },
    {
      "id": "0b0ee3cb-c828-5335-a579-222b7d10c134",
      "collectionId": "2f168ba6-18f3-5f64-b2cb-5d88e2f861af",
      "title": "Back up one table as SQL",
      "body": "pg_dump -d {{database}} -t {{table}} -f {{table}}.sql",
      "description": "",
      "tags": [
        "backup"
      ]
    }
  ]
}
