Search This Blog

Tuesday, May 26, 2015

Postgres SQL: Day to day tips

Dates

transform an int as readable timestamp:

select to_timestamp(FIELD_TO_CONVERT) from table;

Thursday, April 9, 2015

Postgres SQL: Install and first launch

Create and setup the postgres user:
[root@localhost ~]# useradd postgres -m /home/postgres

If you don't have the home dir yet, you can add it at a later stage. You just need to stop all processes used by it first.
[root@localhost ~]# mkdir -p /home/postgres
[root@localhost ~]# chown postgres:postgres /home/postgres
[root@localhost ~]# usermod -d /home/postgres postgres

Then, connect as postgres and start the server:
-bash-4.3$ initdb -D /var/lib/pgsql/data/
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
creating configuration files ... ok
creating template1 database in /var/lib/pgsql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    postgres -D /var/lib/pgsql/data/
or
    pg_ctl -D /var/lib/pgsql/data/ -l logfile start

Start the server:
-bash-4.3$ pg_ctl -D /var/lib/pgsql/data/ -l logfile start

You can now create a database and connect to it:
-bash-4.3$ createdb test1
-bash-4.3$ psql test1
psql (9.3.6)
Type "help" for help.

test1=#


To stop the server:
$ pg_ctl stop




Friday, February 21, 2014

RMAN: Restore controlfiles


Sometimes, your loose all your controlfiles, your instance is down, and you think you're in a big sh**. But here's a solution.


Check your last controlfile backup to get your DBID :
ls -l *.bck
-rw-rw---- 1 oracle oracle   10092544 Apr 15 15:49 c-159519437-20110415-06.bck

The DBID is the first number block : 159519437


Then, go to RMAN and restore your controlfile from the last backup:

rman target /

set DBID=159519437

startup nomount

set controlfile autobackup format for device type disk to '/backup/file/location/%F';

restore controlfile from autobackup;


The last action, recover and start the database

recover database;

alter database open resetlogs;

Tuesday, February 4, 2014

Linux: Generate a random password

The main script, has to be saved in a separate file, or modified as a function:
#!/bin/bash

#add special characters you want to use in the password here:
charspool=('a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p'
'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z' '0' '1' '2' '3' '4' '5' '6' '7'
'8' '9' '0' 'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O'
'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z' '$' '-' '_');

len=${#charspool[*]} if [ $# -lt 1 ]; then         num=$1; else         num=$1; fi randomnumbers=$(head -c $num /dev/urandom | od -t u1 | awk '{for (i = 2; i <= NF; i++) print $i}') for c in $randomnumbers; do         echo -n ${charspool[$((c % len))]} done echo

and to call it:
lenPassword=8
# echo $(/path/to/script/gen_password.sh $lenPassword);

Wednesday, January 29, 2014

Linux: Allow a user to execute only specific root command(s)

Sometimes you want a linux user to be able to execute root command(s). To do so, you need to modify the /etc/sudoers file (with visudo).

However, almost everyone add a line like the below:
username ALL=(ALL:ALL) ALL


Which is far from being the right thing to do. You basically just gave the user username all root rights... To do it properly, you should know the synopsis of that line:
user machine=(effective user [ : effective group ] command


With:
user : the user you want to grant access to some commands. it can be a group too, just replace user by %groupname
machine : the machine you want those rights to apply (can be a hostname, IP, ALL)
effective user : the user's rights you want your user to execute the command as (root, user1, ALL, etc)
effective group : same as above, but for the group. This is not mandatory
command : the command or command list (separated by comma. Ex. /bin/chown,reboot)

An example is allays better than a long talk, if you want to give the user user1 the rights to execute the chown command:
user1 ALL=(root) /bin/chown

Monday, January 27, 2014

Oracle DB ratios explained

Here you will find two pdf (in french and english) with the explanation of the following ratios, and what to do to improve them, as well as your database performances:

  • Buffer Cache Hit Ratio 
  • Chained Row Ratio 
  • Database CPU Time Ratio
  • Database Wait Time Ratio
  • Dictionary Cache Hit Ratio 
  • Execute to Parse Ratio 
  • Get Hit Ratio 
  • Latch Hit Ratio 
  • Library Cache Hit Ratio 
  • Parse CPU to Elapsed Ratio 
  • Pin Hit Ratio 
  • Soft-Parse Ratio 
  • Library cache reload Ratio
  • Rollback Segment Wait to Get Ratio
  • In (PGA) Memory Sort Ratio
  • Buffer Nowait Ratio
  • Redo Nowait Ratio

In french

You can hit the gv$sysmetric view to most of those ratios.

Friday, December 27, 2013

DBMS_LOGMNR, or "How to see what's in the redo logs"


Contents:
1 Database-Level Supplemental Logging
2 Redo Log File Options
3 Example of Querying V$LOGMNR_CONTENTS
4 End the LogMiner Session





Database-Level Supplemental Logging:

ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;



Redo Log File Options

Here you will tell you LOGMNR whate logfiles to look into. It can be done:

Automatically. Just define the time range you want to observe and start logmnr.
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';

EXECUTE DBMS_LOGMNR.START_LOGMNR( STARTTIME => '01-Jan-2003 08:30:00', ENDTIME => '01-Jan-2003 08:45:00', OPTIONS =>
DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG + DBMS_LOGMNR.CONTINUOUS_MINE);


Manually. You need to manually add every logfile you're interested in and then start logmnr.
EXECUTE DBMS_LOGMNR.ADD_LOGFILE( LOGFILENAME => '/oracle/logs/log1.f', OPTIONS => DBMS_LOGMNR.NEW);

EXECUTE DBMS_LOGMNR.ADD_LOGFILE( LOGFILENAME => '/oracle/logs/log.f', OPTIONS => DBMS_LOGMNR.ADDFILE);

EXECUTE DBMS_LOGMNR.START_LOGMNR(OPTIONS => DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG);



Example of Querying V$LOGMNR_CONTENTS


SELECT OPERATION, SQL_REDO, SQL_UNDO
  FROM V$LOGMNR_CONTENTS
  WHERE SEG_OWNER = 'OE' AND SEG_NAME = 'ORDERS' AND
  OPERATION = 'DELETE' AND USERNAME = 'RON';


End the LogMiner Session

EXECUTE DBMS_LOGMNR.END_LOGMNR;



Note: Everything is related to your session, so if you got disconnected, start again

Source: http://download.oracle.com/docs/cd/B28359_01/server.111/b28319/logminer.htm#i1015913