Purge PostgreSQL (Uninstall completely)

In some occasion you may want to uninstall a postgreSQL completely. Here is the procedure:

1. Stop the running postgreSQL instance

/etc/init.d/postgresql stop

2. purge the installation

apt-get --purge remove postgresql\*

3. cleanup


rm -r /etc/postgresql/
rm -r /etc/postgresql-common/
rm -r /var/lib/postgresql/
userdel -r postgres
groupdel postgres

Using AWS EBS as PostgreSQL data space

AWS RDS is good option for running databases as long as you have tons of cash to dump in. If not a better solution is to appropriately combine an EC2 instance with an EBS instance to built your own. And normally in such cases, you want your database data be put into the EBS volume. There are different ways of doing this for different cases

0. Prepare the EBS volume
Starting AWS EC2 with additional EBS volume will only attach the EBS as an accessible device (e.g. /dev/xvdb) but not mount it to the file system. You can do this easily by yourself.


lsblk
sudo file -s /dev/xvdb
sudo mkfs -t ext4 /dev/xvdb
sudo mkdir data
sudo mount /dev/xvdb /data
df

to make the mount automatic on restart

sudo cp /etc/fstab /etc/fstab.orig
sudo vi /etc/fstab

add the following line in “/etc/fstab”

/dev/xvdb /data ext4 defaults,nofail 0 2

the the EBS volume is mounted to “/data” in your file system.

1. For newly installed postgreSQL

You can select the desired data space by modifying the configuration file “/etc/postgresql/9.3/main/postgresql.conf”

Change the line containing

data_directory = ‘/var/lib/postgresql/9.3/main’

to

data_directory = ‘/data/main’

where ‘/data’ is your desired data space path.

2. For installed postgreSQL with database loaded

a. Shut down PostgreSQL
sudo /etc/init.d/postgresql stop

b. Make sure PostgreSQL does not run any longer

c. Check that PostgreSQL is really stopped
ps -ef | grep postgres

should see no processes running.

d. Copy the old data directory to the new drive

e. Update your PostgreSQL configuration to point to the new data directory by changing “/etc/postgresql/9.3/main/postgresql.conf”

Change the line containing

data_directory = ‘/var/lib/postgresql/9.3/main’

to

data_directory = ‘/data/main’

where ‘/data’ is your desired data space path.

and

sudo chown -R postgres:postgres /data

f. If you copied the data, rename the old data directory to make sure PostgreSQL cannot find it any longer. That way you’ll get an error message if you didn’t get the configuration right.

g. Start the service
sudo /etc/init.d/postgresql start

h. If everything is running fine, you can delete the old directory.

3. make use of postgreSQL table space

create table space in your desired volume
create database tables in the new table space.

Setting up PostGIS on AWS RDS PostgreSQL Database Server

The greatest capability of PostgreSQL database, in my opinion, is the PostGIS extension. The AWS RDS creation procedures for PostgreSQL does not set up this extension by default. The procedures described in the following AWS instructions is very thorough on how to setup the PostGIS through a PostgreSQL client using commands:

http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Appendix.PostgreSQL.CommonDBATasks.html#Appendix.PostgreSQL.CommonDBATasks.PostGIS

Here I noted the similar procedures run on pgAdmin III PostgreSQL GUI admin tool.

1. Add the RDS database server to pgAdmin III managed database server list
– server host name
– server port
– maintenance database name
– master user name
– master user password

2. Connect to the RDS database server
– select the maintenance database
– click the “Query” button to start the query window
– in the query window, run the following commands in sequence
+ select current_user; (you should see the master user name as the query return)
+ create extension postgis;
+ create extension fuzzystrmatch;
+ create extension postgis_tiger_geocoder;
+ create extension postgis_topology;
+ alter schema tiger owner to rds_superuser;
+ alter schema topology owner to rds_superuser;
CREATE FUNCTION exec(text) returns text language plpgsql volatile AS $f$ BEGIN EXECUTE $1; RETURN $1; END; $f$;
+ SELECT exec(‘ALTER TABLE ‘ || quote_ident(s.nspname) || ‘.’ || quote_ident(s.relname) || ‘ OWNER TO rds_superuser’)
FROM (
SELECT nspname, relname
FROM pg_class c JOIN pg_namespace n ON (c.relnamespace = n.oid)
WHERE nspname in (‘tiger’,’topology’) AND
relkind IN (‘r’,’S’,’v’) ORDER BY relkind = ‘S’)
s;

+ select na.address, na.streetname, na.streettypeabbrev, na.zip from normalize_address(‘1 Devonshire Place, Boston, MA 02109’) as na;

Renaming a git repository on server and repository mirror

If you created your repository and started pushing contents to it and some day later you do not like the repository name any more (or it is not liked by some one else) and thus you want to rename it. You can actually do it simply by changing a couple of configuration files, depends on what it is.

1. rename the repository on server

1.1 just simply rename the repository folder name by

$ mv old-repository.git new-repository.git

2. rename the repository mirror agent

In case you have set up a repository mirroring agent in your machine conducting a daily mirroring of your server repository to a backup server, you can do the rename for the repository mirroring agent as folows:

2.1. rename the repository folder name by

$mv old-repository.git new-repository.git

2.2. $cd new-repository.git

2.3. modify all appearances of “old-repository” in “config” file to “new-repository”

2.4. modify all appearances of “old-repository” in “FETCH_HEAD” file to “new-repository”

3. rename the local clone of repository

3.1 change the clone root folder name

$mv old-repository new-repository

3.2 $cd new-repository/.git

3.3. modify all appearances of “old-repository” in “config” file to “new-repository”

Install Git and Maven for Cygwin

Cygwin is a very convenient and powerful Linux/Unix emulation tool for Windows. It supports a large collection of GNU and Open Source tools providing functionality similar to Linux/Unix distribution on Windows. Even though it is not built for running native Linux apps on Windows directly, it does magically make native Windows apps aware of Linux/UNIX® functionality like signals, ptys, etc.. For both Linux/Unix utilities to run in Cygwin and Windows apps to take advantage of Cygwin functionality, they all  need to be rebuilt from source codes. Fortunately, Cygwin already has the majority and most popular ones in the package allowing installation time customization.

For software development, especially Java, git and maven are very useful and common tools for version controlling the source codes and building runtime executables.

1. Install Git for Cygwin

Git is already included in the current Cygwin distribution package (https://www.cygwin.com/setup-x86_64.exe or https://www.cygwin.com/setup-x86.exe for 64-bit and 32-bit Windows separately), and therefore installing Git can be easily done at the Cygwin installation time by enabling all “git” related packages. If you did not do this at Cygwin installation time, you can always add them in by rerun the Cygwin installation but only select “git” related packages:

2.1 type in “git” in the search bar in Cygwin installation window,
2.2 click on the + sign to show details of all found packages, then
2.3 click on the double-circle-arrow sign of a package,
2.4 change from “skip” to “install” (if they are already installed, the options will be “keep”, “reinstall” or “uninstall”) and then
2.5 continue the rest of installation process.

After installation, git is ready to run.

2. Install Maven

Maven installation is not as “native” as installing git. Follow below procedures to install.

2.1 Download the specific or latest version of “Maven” for Window from the official Apache maven project site (http://maven.apache.org/guides/getting-started/windows-prerequisites.html)

2.2 Unzip/Decompress the download file to your prefered file path, e.g. simply “C:”, then you will have a maven folder/directory “C:\apache-maven-x.y.z” containing all maven required files.

2.3 Setup the Windows environment variable “Path” to point to the newly installed maven

Start Windows “Control Panel -> System” select “Advanced System Settings -> Advanced”, click on “Environment Variables”, edit the “Path” variable of either “User variables for xxx” or “System variables” by adding “C:\apache-maven-x.y.z\bin”.

2.4 Verify mavan accessibility in Cygwin

Start Cygwin shell, type in “which mvn”, you should see “/cygdrive/c/apache-maven-x.y.z/bin/mvn”, then maven is installed and ready to run. If “mvn” command is not available in Cygwin shell, restart your PC.

Duplicating A Git Repository

Often times, you want to duplicate your git repository for redundancy or some other reasons. There are normally two ways of doing this:

1. Making an exact duplicated repository

In this case, you are duplicating your repository to a new location, and therefore you are having two duplicated but really independent repositories. If you want to maintain the synchronization of the two repositories, you will have to execute the same set of push commands separately to the two repositories. So this method is really good for the cases that you are moving your repositories to a new server and going to eventually discard the old repository.

Procedures of making exact duplicated repository are

1.1 bare-clone

Make a bare clone of the repository to be deplicated

$ git clone –bare https://github.com/exampleuser/old-repository.git

1.2 mirror-push

Assume you did above clone in “/home/user/you/github/”, then under this directory, do

$ cd old-repository.git
$ git push –mirror https://github.com/exampleuser/new-repository.git

This will duplicate your repository to a new repository. You can duplicate to a different server by setting a different mirror location. For example, if you want to duplicate to your local host, you can do this by e.g.

$ git push –mirror james@localhost:/media/james/FW/repositories/github/new-repository.git

[Note]: You need to initialize the git repository in your new location if it does not exist by issuing the following commands (assume you are initializing a local repository “new-repository” under you local host “/media/james/FW/repositories/github/”

$ cd /media/james/FW/repositories/github/
$ mkdir new-repository.git
$ cd new-repository.git
$ git --bare init
$ git config core.sharedrepository 1
$ git config receive.denyNonFastforwards true
$ find objects -type d -exec chmod 02770 {} \;

For maintaining multiple mirrored repositories that can be synchronized using simple procedures, you will have to use a different approach as described in 2.

2. Mirroring

If you want mirror a repository in another location, including getting updates from the original, you can clone a mirror and periodically push the changes.

2.1. Make a bare mirrored clone of the repository (Mirror the repository with all branches and tags)

$ git clone –mirror [old-repository-Url], e.g.

$ git clone –mirror https://github.com/exampleuser/repository-to-mirror.git

2.2 Set the push location to your mirror

Assume the cloned local repository mirror is repository-to-mirror.git, and the mirrored repository is james@localhost:/media/james/FW/repositories/github/mirrored-repository.git

$ cd repository-to-mirror.git
$ git remote set-url –push origin james@localhost:/media/james/FW/repositories/github/mirrored-repository.git

or edit the “config” under “repository-to-mirror.git” to have contents as below:

[core]
repositoryformatversion = 0
filemode = true
bare = true
[remote “origin”]
url = http://github.com/exampleuser/repository-to-mirror.git
fetch = +refs/*:refs/*
mirror = true
pushurl = james@localhost:/media/james/FW/repositories/github/mirrored-repository.git

This will set the push url of origin to the mirrored repository and keep the pull url of origin to the original repository. Then you can issue the following commands to pull changes from the original repository and push to the mirrored repository in this middle mirrored agent “repository-to-mirror.git”:

$ git fetch -p origin
$ git push –mirror

(original repository) — pull –> (middle agent) — push –> (mirrored repository)

Optionally, you can add a new remote to the new repository (you need to initialize the new git repository), and do the push explicitly.

git remote add remoteName newRepoUrl

2.3 Push all to the new repository (Alternative option)

2.2 shall do all for the mirroring. The following command is another way of pushing to the remote mirror repository.

git push -f –tags remoteName refs/heads/*:refs/heads/*

3. Duplicate only the most up-to-date snapshot

3.1 in you local git workspace

cd path/to/local/repo

3.2 remove the old default remote “origin”

git remote remove origin

3.2 setup the new default remote “origin”

git remote add origin newRepoUrl

3.3 push all branches and tags

git push -u origin –all
git push -u origin –tags

Echo text in color: Linux — Ubuntu

Often a time, you may need to echo text in different color on terminal. Here is the way that Linux/Ubuntu works

1. set color and echo

echo -e "\E[1;32mHello World"

The text color and format is defined by the format string “\E[1;32” and option “-e” turns the feature on.

The first number in the format string specifies the display format, example options are

0=none, 1=bold, 4=underscore, 5=blink, 7=reverse, 8=concealed

The second number in the format string specifies the color, example options are

30=black, 31=red, 32=green, 33=yellow, 34=blue, 35=magenta, 36=cyan, 37=white

2. reset back to normal text, run

tput sgr0

Worknotes: Eclipse C++ Project

1. Create C++ project

File -> New -> C++ Project

Type in project name, select “Use default location” if new empty project and you want to use the default location, or deselect “Use default location” and type in you preferred location in “Location” — this also allows to create project on existing C++ codes.

2. Add/Modify include paths

right click project, select “Properties” -> “C/C++ General” -> “Paths and Symbols”, select “GNU C++” in Languages and click “Add” to add desired include paths.

The path included shall be shown as

/usr/local/xxxx/include

Note: these only applies to all common library include paths, not the project local include path.

3. set project local include path

right click project, select “Properties” -> “C/C++ Build” -> “Settings”, select GCC C++ Complier -> includes”, click the green “+” icon in “inlcude paths (-I)” window. Click on “Workplace” button in “Add directory path” pop-up window. navigates to the right project local “include” directory, then “OK”. The project local include path shall be shown as

"${workspace_loc:/${ProjName}/include}"

Viewing Log4j Event Log on Web

Event log is a great resource for tracking run time issues on fly. However, most of the event log viewer available are GUI applications. In some situations, a web based GUI log viewer will be a better and convenient tool, especially when people start paying more and more attentions to clouding their services and products.

This note records the process of finding a good open source web based log viewer for logs generated by e.g. log4j.

1. Direct logs to Database such as MongoDb using event log database appender. In my application, I used a mongoDb appender “org.log4mongo.MongoDbAppender” and a web based MongoDb Management tool “mVierer”. This combination satisfied my basic need. However, if I need advanced analytic capabilities, customized MongoDB query logic and dedicated web GUI is still needed.

2. “Log4Web” is a tool developed by Launch Software. This web application reads log4j generated xml formatted log files (with .log4j extension) and presents on the web.

Log4Web is provided in .war format and can easily be installed in any web server containers such as Apache Tomcat. After installation, the log file reading page is

http://:/Log4Web/

to make this whole thing work, the following are needed

2.1. make the application file log appender to log in xml format (see the following exampe)

<appender name="xmlAppender-rsas" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="INFO" />
<param name="append" value="true"/>
<param name="File" value="/var/log/xxx/fw_rsas.log4j"/>
<param name="MaxFileSize" value="10MB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.xml.XMLLayout">
<param name="LocationInfo" value="true"/>
</layout>
</appender>

2.2. config Log4Web log file

2.2.1 import the .war file into eclipse

File –> import
select “Web/WARfile” in templates list
and then follow the steps

2.2.2 edit the configuration file

edit the web.xml file contained in Log4Web.war (Log4Web/WebContent/WEB-INF/lib/web.xml), and add the specification of the log file directory, and repackage the .war.

2.2.3 repackage the .war

Right click “Log4Web” project, Export –> WAR file

2.3. deploy the new .war file into web server container.

Everything looks working except the web page have a limit of the log data not late than 2010. I’ll find out how to fix this later.