Skip to content

Instantly share code, notes, and snippets.

@Blankeos
Last active August 22, 2024 18:53
Show Gist options
  • Select an option

  • Save Blankeos/b1417dca70fa519ca0ce949b76d32e1a to your computer and use it in GitHub Desktop.

Select an option

Save Blankeos/b1417dca70fa519ca0ce949b76d32e1a to your computer and use it in GitHub Desktop.
PostgreSQL Cheatsheet

Run PostgreSQL

Just use Docker-Compose

    // package.json scripts
    "db:start": "docker-compose up -d --build",
    "db:stop": "docker-compose stop",
# docker-compose.yml
version: "3"
name: <main_container_name> # Project name container name (otherwise it will use the folder name)
services:
  db: 
    container_name: <db_container_name> # Subcontainer name: <main_container_name>_db_1
    image: postgis/postgis
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password123
      # Creates a new "Database" of this name only
      # if no initial databases exist. It only runs the first time.
      POSTGRES_DB: <db_name> # Your connection string will be: postgresql://postgres:[email protected]:5432/<db_name>
    volumes:
      - ./data:/var/lib/postgresql/data
    ports:
      - "5432:5432"

PSQL

docker exec -ti <db_container_name> - Prefix your commands with this if db is in Docker.
psql -U postgres - Login

\l - List databases
\c - Connect to database
\dn - List schemas inside database
\dt - List tables inside public schemas
\dt schema1.* - List tables inside a particular schema.
                For example: 'schema1'.
\d - Display schema of a table
\d+ - Display schema of a table + more info.

create database <db_name>; - create a database

PostgreSQL

# Describe A Table
SELECT * FROM information_schema.columns where table_name = '<table name>';

PostGIS

Note

Work in Progress

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment