Sunday, November 18, 2012

PSQL 101

How to create a database
CREATE DATABASE mydatabase;

How to create a user
CREATE USER priyank with password 'passw0rd';

How to grant access to a user
GRANT ALL PRIVILEGES ON DATABASE mydatabase to priyank

How to see list of databases
psql -l -U priyank
Alternatively you can connect to database first and then type '\l'
\l

How to see list of tables
#Connect to database first.
\d

Friday, November 9, 2012

Shell: How to increment a value in a shell script

This is a very basic question that comes into our mind many times while writing a shell script.

Here is a small code that you may find it useful.

#!/bin/sh
i=0
j=5
while [ $i -le $j ]
do
i=`expr $i + 1`
echo $i
done

Here I am using expr unix command. This command evaluates arguments as expressions. You can find more details about expr by typing man expr in your terminal or check this link for online manual.