Last active
July 15, 2025 14:06
-
-
Save AndrianBdn/7ee91d88815a1ec5b2ace465dcef7b6d to your computer and use it in GitHub Desktop.
create-tb-1000-schema-enum.sh
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 characters
| #!/bin/bash | |
| set -e | |
| CONTAINER_NAME="pg-tableplus-test" | |
| POSTGRES_PASSWORD="testpass" | |
| POSTGRES_USER="testuser" | |
| POSTGRES_DB="testdb" | |
| PORT="5433" | |
| SQL_FILE="setup_1000_schemas.sql" | |
| # Clean up function | |
| cleanup() { | |
| echo "=== Cleaning up: stopping and removing container ===" | |
| docker stop $CONTAINER_NAME >/dev/null 2>&1 || true | |
| docker rm $CONTAINER_NAME >/dev/null 2>&1 || true | |
| rm -f $SQL_FILE | |
| } | |
| trap cleanup EXIT | |
| echo "=== 1. Starting PostgreSQL Docker container ===" | |
| docker run --name $CONTAINER_NAME -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD -e POSTGRES_USER=$POSTGRES_USER -e POSTGRES_DB=$POSTGRES_DB -p $PORT:5432 -d postgres:16 | |
| echo "=== 2. Waiting for PostgreSQL to be ready... ===" | |
| for i in {1..20}; do | |
| docker exec $CONTAINER_NAME pg_isready -U $POSTGRES_USER && break | |
| sleep 1 | |
| done | |
| echo "=== 3. Preparing big SQL script ($SQL_FILE) ===" | |
| rm -f $SQL_FILE | |
| for i in $(seq 1 1000); do | |
| SCHEMA="schema_$i" | |
| cat >> $SQL_FILE <<EOF | |
| CREATE SCHEMA $SCHEMA; | |
| SET search_path TO $SCHEMA; | |
| CREATE TYPE test_type AS ENUM ('foo', 'bar'); | |
| CREATE TABLE test_table ( | |
| id serial PRIMARY KEY, | |
| content text, | |
| type test_type | |
| ); | |
| EOF | |
| done | |
| echo "=== 4. Copying SQL file into container ===" | |
| docker cp $SQL_FILE $CONTAINER_NAME:/tmp/$SQL_FILE | |
| echo "=== 5. Executing SQL in container ===" | |
| docker exec -u postgres $CONTAINER_NAME psql -U $POSTGRES_USER -d $POSTGRES_DB -f /tmp/$SQL_FILE | |
| echo "=== 6. Waiting for you to finish debugging. Stop the container (CTRL+C or docker stop) when done. ===" | |
| echo "Connect to PostgreSQL with: host=localhost port=$PORT user=$POSTGRES_USER password=$POSTGRES_PASSWORD database=$POSTGRES_DB" | |
| # This will wait for the container to stop, while the EXIT trap will handle cleanup. | |
| docker wait $CONTAINER_NAME | |
| echo "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment