środa, 13 czerwca 2012

How to obtain postgresql database size

If you want to find the size of a dataase , the simples method is use query : 

SELECT pg_database_size(‘databasename’);


postgres=# SELECT pg_database_size('aad');
 pg_database_size
------------------
       4582268508
(1 row)



For more human readable format please use:

SELECT pg_size_pretty(pg_database_size(‘databasename’));


postgres=# SELECT pg_size_pretty(pg_database_size('aad'));
 pg_size_pretty
----------------
 4370 MB
(1 row)

To get the size of all databases with alphabetical order:
 
SELECT datname,pg_size_pretty(pg_database_size(pg_database.datname)) FROM  pg_database ORDER BY datname;


postgres=# SELECT datname,pg_size_pretty(pg_database_size(pg_database.datname)) FROM  pg_database ORDER BY datname;
      datname      | pg_size_pretty
-------------------+----------------
 aad              | 4370 MB
 bae     | 4797 MB
 maad    | 534 MB
 pjvi            | 11 MB
 template0         | 4248 kB
 template1         | 4248 kB
 (6 rows)

poniedziałek, 14 maja 2012

Move table to another tablespace in Postgresql

dbtest# \t
dbtest# \o dbtest_tablespace.txt
dbtest# select 'alter table '||tablename||' set tablespace temporary_tablespace;' from pg_tables where tablename like 'example%';
dbtest# select 'alter index '||indexname||'  set tablespace temporary_tablespace;' from pg_indexes where tablename like 'example%';
dbtest# \i dbtest_tablespace.txt

środa, 2 maja 2012

FusionIO kernel linux 3.2 patch

Recently I've got problems with fusionIO and linux new kernel because fusionIO doesn't support new kernels (> 2.6). Below is patch for kernel 3.2 ( I think it should work on 3.x too). FusionIO kernel patch

czwartek, 19 kwietnia 2012

Log coloring with bash

Below is link to short script I've made to see different logs with highlight different patterns.
How to use it ?


shamrock@alucard:~/dev/shell$ ./cgrep.sh
Usage : ./cgrep.sh <-g|--grep,-l|--less,-m|--more,-t|--tail> file [line|pattern]
-g, --grep - use grep with highlight
-l, --less - use less with highlight
-m, --more - use more with highlight
-t, --tail - use tail with highlight
line - highlights whole line
pattern - highlights whole pattern
Available colours: black,red,green,yellow,blue,magenta,cyan,white
Available bold colours: blackb,redb,greenb,yellowb,blueb,magentab,cyanb,whiteb

It is possible to use sed different patterns inside quotation marks i.e.:

cgrep sample.log pattern redb "Process target" blueb [Cc]urrent magentab "[0-9]\{3,10\}"


How doe it work in practice?


shamrock@alucard:~/dev/shell$ cat test
Lorem ipsum
Test lorem ipsum
test test lorem ipsum
shamrock@alucard:~/dev/shell$ ./cgrep.sh --grep test redb [Ll]orem

Lorem ipsum
Test
lorem ipsum
test test
lorem ipsum


cgrep.sh

piątek, 10 lutego 2012

Recover mysql/postgresql root password

To recover MySQL password you simply need to 

1) shutdown mysql database if it's possible or just kill with TERM 
In Linux :

# service mysqld shutdown
For killing process
# kill -TERM `ps axxwww | grep mysqld`

2) Run database with "skip grants" option


# /bin/sh /usr/bin/mysqld_safe --defaults-file=/etc/my.cnf \
--pid-file=/var/run/mysqld/mysqld.pid --skip-grant-tables

3) Log in into database and change password
# mysql 
mysql> use mysql;
mysql> update user set password=PASSWORD("newpassword") where user="root";
mysql> quit;
 
4) Restart database and log in with new password
 
To recover Postgresql password
 
1) Edit postgresql config file pg_hba.conf  and change line from :
 local   all postgres  peer
to 
local all postgres trust
 
2) Restart postgresql server :
# service postgresql restart 

3) Log in into database and change password
# psql -U postgres
postgres=# use template1
postgres-# alter user postgres with password 'secret';
postgres-# \q

4) Restart postgresql server

niedziela, 8 stycznia 2012

Vim tips

Converting TAB to SPACE

:set et
:ret!
or convert all SPACE to TAB
:set et!
:ret! 
 
Joining lines based on pattern:
 
global join:
 :g/pattern/j 

single join:
 :/pattern/j 

Some useful things to .vimrc

set cursorline                  " highlight current line
hi cursorline guibg=#333333     " highlight bg color of current line
"hi tab guibg=#000333     " highlight bg color of current line
hi CursorColumn guibg=#333333   " highlight cursor

syntax on

sobota, 24 grudnia 2011

MySQL - connection class in python

Simple class allowing connect to mysql databse and gather/insert data into it.

Import class system and MySQLdb


import sys
import MySQLdb


Connect to database - if an error occures display message and exit.


def __init__(self,host,user,password,database):
self.database_connect(host,user,password,database)
return

def database_connect(self,host,user,password,database):
try:
self.conn = MySQLdb.connect(host=host, user=user, passwd=password, db=database)
except MySQLdb.Error, e:
print "Connection error\n%d: %s\n" % (e.args[0],e.args[1])
sys.exit(1)
return

Try to connect to database and return cursor.


def cursor_return(self):
try:
self.conn.ping()
except:
self.database_connect()
return self.conn.cursor()


Get one result from database.


def result_return(self,query):
kursor = self.cursor_return()
try:
kursor.execute(query)
except MySQLdb.Error, e:
print "Query error\n%s\n%d: %s\n" % (query,e.args[0],e.args[1])
sys.exit(1)

result = kursor.fetchone()
kursor.close()
return result


Get results from database.


def results_return(self,query):
kursor = self.cursor_return()
try:
kursor.execute(query)
except MySQLdb.Error, e:
print "Query error\n%s\n%d: %s\n" % (query,e.args[0],e.args[1])
sys.exit(1)

results = kursor.fetchall()
kursor.close()
return results


Execute query.


def execute_query(self,query):
kursor = self.cursor_return()
try:
kursor.execute(query)
except MySQLdb.Error, e:
print "Query error\n%s\n%d: %s\n" % (query,e.args[0],e.args[1])
sys.exit(1)
kursor.close()
return


How to us it?



import DBMySQL

c=DBMySQL.DataBase("hostname","username","password","database_example")

t = c.results_return("SELECT id,title FROM films;")

for (i,j) in t:
print "%s %s " % (i,j)



1 Transporter
2 Taxi 1
3 Taxi 2
4 Taxi 3
5 Alien 2
6 Alien 1
7 Alien 3
8 Alien 4
9 The Thing
10 Reservoir dogs

Source code