IMPORTANT!

Snipt is going open source. We've toyed with this idea for quite a while, and have finally decided it's the right way to move forward.

A few things:
  • The entire Snipt source code will be released on GitHub under the 3-clause BSD License on Friday, September 10th.
  • While we'd like to think we're perfect, we realize we're only human. By open sourcing the software that runs this website, certain bugs or security flaws may be discovered that could compromise the privacy of your snipts.
  • Only the Lion Burger team will be able to push commits to the Snipt.net site. Contributors should send a pull request to add new features or submit patches.
  • By using this site, you agree not to be too angry or take any legal action against Lion Burger should this whole thing go up in flames some day.
  • Follow us on Twitter for updates.
I agree, close this message
Sign up to create your own snipts, or login.

Latest 100 public snipts » Fotinakis's snipts » postgresql The latest postgresql snipts from Fotinakis.

showing 1-4 of 4 snipts for postgresql
  • Output PostgreSQL command as CSV
    psql -F\; -A --pset footer -f input_file.sql -o output_file.csv database_name
    

    copy | embed

    0 comments - tagged in  posted by Fotinakis on Nov 10, 2009 at 4:04 p.m. EST
  • Make copy and transfer entire PostgreSQL DB
    pg_dump -d prod_db -U prod_db -h localhost > /tmp/transferdump.sql
    
    psql -U prod_db_user -h localhost -t -d other_db -c "SELECT 'DROP TABLE ' || n.nspname || '.' ||
    c.relname || ' CASCADE;' FROM pg_catalog.pg_class AS c LEFT JOIN
    pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace WHERE relkind =
    'r' AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND
    pg_catalog.pg_table_is_visible(c.oid)" >/tmp/dropothertables.sql;
    
    psql -d other_db -h localhost -U other_db_user < /tmp/dropothertables.sql;
    psql -d other_db -h localhost -U other_db_user < /tmp/transferdump.sql
    

    copy | embed

    0 comments - tagged in  posted by Fotinakis on Jul 28, 2009 at 6:15 p.m. EDT
  • Drop all tables from PostgreSQL DB without superuser
    psql -t -d my_dbname -c "SELECT 'DROP TABLE ' || n.nspname || '.' ||
    c.relname || ' CASCADE;' FROM pg_catalog.pg_class AS c LEFT JOIN
    pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace WHERE relkind =
    'r' AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND
    pg_catalog.pg_table_is_visible(c.oid)" >/tmp/droptables
    
    psql -d my_dbname -f /tmp/droptables
    

    copy | embed

    0 comments - tagged in  posted by Fotinakis on Apr 28, 2009 at 12:09 p.m. EDT
  • Grant all permissions on PostgreSQL DB
    ER="$2"
    PSQL="psql -q -n -A -t"
    SCHEMES="'public'"
    
    if [ -z "$1" ]; then
            echo "Something like: ./grant mydatabase myuser | psql mydatabase"
            exit
    fi
    
    if [ -z "$2" ]; then
            USER="$1"
    fi
    echo "-- Granting rights on $DB to $USER ($SCHEMES)"
    # tables
    Q="select 'grant all on '||schemaname||'.'||tablename||' to \\\"$USER\\\";' from pg_tables where schemaname in ($SCHEMES);"
    $PSQL -c "$Q" "$DB";
    
    # views
    Q="select 'grant all on '||schemaname||'.'||viewname||' to \\\"$USER\\\";' from pg_views where schemaname in ($SCHEMES);"
    $PSQL -c "$Q" "$DB";
    
    # sequences
    Q="select 'grant all on function '||n.nspname||'.'||p.proname||'('||oidvectortypes(p.proargtypes)||') to \\\"$USER\\\";' from pg_proc p, pg_namespace n where n.oid = p.pronamespace and n.nspname in ($SCHEMES);"
    $PSQL -c "$Q" "$DB";
    
    # functions
    Q="select 'grant all on '||n.nspname||'.'||c.relname||' to \\\"$USER\\\";' from pg_class c, pg_namespace n where n.oid = c.relnamespace and c.relkind IN ('S') and n.nspname in ($SCHEMES);"
    $PSQL -c "$Q" "$DB";
    

    copy | embed

    0 comments - tagged in  posted by Fotinakis on Apr 28, 2009 at 12:05 p.m. EDT
Sign up to create your own snipts, or login.