Change owner of all the tables and sequences

for schema in 'public' 'firstschema' 'secondschema'
do
    for tbl in `psql -h hostname -U postgres -qAt -c "select tablename from pg_tables where schemaname = '$schema';" bddname`
    do
        psql -h hostname -U postgres -c "alter table ${schema}.$tbl owner to username" bddname
    done
done
for schema in 'public' 'firstschema' 'secondschema'
do
    for ...
more ...

Database dump using gzip

#!/bin/bash

for bdd in 'dbname01' 'dbname02' 'dbname03'
do
    `echo "Procesando $bdd" >> salida.log`
    `echo "Procesando $bdd" >> error.log`
    `pg_dump -C -Fp -h hostname -U postgres ${bdd}  | gzip -c > ${bdd}_dump.sql.gz `
    #`gunzip < ${bdd}_dump.sql.gz | psql -h hostname -U postgres postgres -f - > salida_restore.log`
done

For performing ...

more ...

Grant for a user on all tables

#!/bin/bash

for table in `echo "SELECT relname FROM pg_stat_all_tables;" | psql dbname | grep -v "pg_" | grep "^ "`;
do
    echo "GRANT ALL ON TABLE $table to username;"
    echo "GRANT ALL ON TABLE $table to username;" | psql dbname
done
more ...

Moving the database cluster

Stop the postgres service

Create the folder for the new cluster and assing as proprietary to the user and group postgres [/home/postgres/data]

Run as postgres user

$ initdb -D /home/postgres/data

Change several things in /etc/postgresql/9.1/main/postgres.cnf:

  • Allowed conexions
  • Data dir to /home ...
more ...

Comparing text files in bash

I wanted to compare two very large text files, one with 5M rows and another with 10M, I was seeking for the lines that where present in both files.

First of all, both files must be sorted, and the you can use the comm command.

# sort a.txt > sort_a.txt ...
more ...


Disk formating using FAT32

We are using a series of commands

# fdisk /dev/sdf

Using this options in order:

n - Nueva tabla
p - Partición primaria
1 - Solo una partición
- Aceptamos las opciones por defecto -
w - Grabamos la nueva tabla

We can return to the menu anytime with:

m - Menú de la aplicación

For the ...

more ...