kill a running query in postgres

There are queries which takes more times to execute. Sometimes you need to stop the queries but you can’t do it simply(Ctrl+c) because queries are coming from application. There are mainly two approaches to kill the query. You can find the process corresponding to the query & kill or you can find the procpid of the query and terminate it from PostgreSQL console. Former approach may not be a great idea cause PostgreSQL thinks there is a crash in the system & restarts the whole PostgreSQL database. for the later approach you need to do the following steps. Get the Status of the running queries. To get the details about all the running on the PostgreSQL,  please execute below query.

SELECT * FROM pg_stat_activity;

or

SELECT procpid,current_query from pg_stat_activity;

Get procid of the query you want to kill / Stop. Stop the query by below query:

SELECT pg_cancel_backend(procpid);

Kill the database connection by below query:

SELECT pg_terminate_backend(procpid);

MySQL query with the conditions as ‘NOT LIKE IN’

While working with MySQL database i needed to write a query which will exclude rows where a column contains one or more words from a set of words. i.e I want to fetch data from student from table where name doesn’t contain words shashank,john etc.

Initially i was trying to write a query like this  :

select * from TABLE
where
Column NOT Like IN
('%VALUE1%', '%VALUE2%', '%VALUE3%') 

but i was getting error as IN doesn’t work with Like.

There is two way you can write this query.

Method 1 :

Add multiple Like statement, each for every word to be excluded.

Select * from TABLE
where Column NOT Like '%VALUE1%'
AND Column NOT Like '%VALUE2%'
AND Column NOT Like '%VALUE3%' 

Method 2 :

If you have set of words which you want to include/exclude in search from a particular column. You may want to use regular expression function of mysql.

Exclude set of words from a column :

SELECT
*
FROM
TABLE
WHERE
Column NOT REGEXP 'VALUE1|VALUE2|VALUE3' 

Search set of words in a column :

SELECT
*
FROM
TABLE
WHERE
Column REGEXP 'VALUE1|VALUE2|VALUE3'

switching back to text console error during centos installation

I was trying to dual-boot CentOS 6 on my laptop that is currently running Windows 8. I have two storage devices, an SSD that has my Windows installation, and an HDD that has all of my data. Both are formatted using GPT, and Windows boots using UEFI. I used the CentOS 6.5 live CD (CentOS-6.5-x86_64-LiveCD.iso) to create an EFI-bootable flash drive (it does boot properly in EFI mode). I receive an error, however, when CentOS is booting (error is below).

Kernel panic - not syncing: Attempted to kill init!
Pid: 1, comm: init Not tainted 2.6.32-431.el6.x86_64 #1
Call Trace:
[<ffffffff815271fa>] ? panic+0xa7/0x16f
[<ffffffff81077622>] ? do_exit+0x862/0x870
[<ffffffff8118a865>] ? fput+0x25/0x30
[<ffffffff81077688>] ? do_group_exit+0x58/0xd0
[<ffffffff81077717>] ? sys_exit_group+0x17/0x20
[<ffffffff8100b072>] ? system_call_fastpath+0x16/0x1b
drm_kms_helper: panic occurred, switching back to text console

Solution : To fix this, go under the EFI folder in your USB stick, then find those files ending with *.conf and use a text editor to change the root= to your USB device. In my case, it is some things like live:UUID=UUID_OF_Partition. How to find UUID of a USB Drive : you can use the following command if you have a linux system.

#sudo blkid
/dev/sda1: UUID="69C2-59F8" TYPE="vfat"
/dev/sda2: UUID="3378cbd8-7df1-451f-b475-d3dbf95edf35" TYPE="ext4"
/dev/sda3: UUID="dd505d50-43e8-4d5c-9543-4df4edbff790" TYPE="swap" 

Or you can go the properties of your USB Drive & name will be the UUID of that USB Drive.
Name :   UUID=69C2-59F8

Then your content to put in *.conf  files will become : live:UUID=69C2-59F8. Another note to take care is instead of install using UNetBootIn or ISO2USB, we can actually use Fedora Live USB Creator. The good thing about use linux is that you can find your USB device’s UUID under syslinux\syslinux.cfg.

PHP cURL

cURL Requests with PHP

CURL is a way you can hit a URL from your code to get a HTML response from it. cURL means client URL which allows you to connect with other URLS and use there responses in your code. Introduction cURL allows transfer of data across a wide variety of protocols, and is a very powerful system. It’s widely used as a way to send data across websites, including things like API interaction and oAuth. cURL is unrestricted in what it can do, from the basic HTTP request, to the more complex FTP upload or interaction with an authentication enclosed HTTPS site. We’ll be looking at the simple difference between sending a GET and POST request and dealing with the returned response, as well as highlighting some useful parameters. Basics Before we can do anything with a cURL request, we need to first instantiate an instance of cURL – we can do this by calling the function curl_init();, which returns a cURL resource. This function takes one parameter which is the URL that you want to send the request to, however, in our case, we’ll hold off doing that for now and set it an alternatively way later. Settings Once we’ve got a cURL resource, we can begin to assign some settings, below is a list of some of the core ones that I set

  • CURLOPT_RETURNTRANSFER – Return the response as a string instead of outputting it to the screen
  • CURLOPT_CONNECTTIMEOUT – Number of seconds to spend attempting to connect
  • CURLOPT_TIMEOUT – Number of seconds to allow cURL to execute
  • CURLOPT_USERAGENT – Useragent string to use for request
  • CURLOPT_URL – URL to send request to
  • CURLOPT_POST – Send request as POST
  • CURLOPT_POSTFIELDS – Array of data to POST in request

We can set a setting by using the curl_setopt() method, which takes three parameters, the cURL resource, the setting and the value. So, to set the URL that we’re sending the request to as http://curlTest.com:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://curlTest.com');

As mentioned, we can set the URL by sending a parameter through when getting the cURL resource:

$curl = curl_init('http://curlTest.com');

It is possible to set multiple settings at one time by passing through an array of settings and values to the function curl_setopt_array():

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://curlTest.com'
));

Sending request When all of the options are sent, and the request is ready to send, we can call the curl_exec() method which will execute the cURL request. This function can return three different things: false – if there is an error executing the request true – if the request executed without error and CURLOPT_RETURNTRANSFER is set to false The result – if the request executed without error and CURLOPT_RETURNTRANSFER is set to true Using the previous example, where we are wanting to get the result back, we would use the following:

$result = curl_exec($curl);

With $result now containing the response from the page – which might be JSON, a string or a full blown site’s HTML. Close Request When you’ve sent a request and got the result back, you should look to close the cURL request so that you can free up some system resources, this is as simple as calling the curl_close() method which as with all other functions takes the resource as its parameter. GET Request A GET request is the default request method that is used, and is very straight forward to use, infact all of the examples so far have been GET requests. If you want to send parameters along in the request you simply append them to the URL as a query string such as http://curlTest.com/?item1=value&item2=value2. So for example to send a GET request to the above URL and return the result we would use:

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://curlTest.com/?item1=value&item2=value2',
    CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

POST Request The sole difference between the POST and GET request syntax is the addition of one setting, two if you want to send some data. We’ll be setting CURLOPT_POST to true and sending an array of data through with the setting CURLOPT_POSTFIELDS So for example switching the above GET request to be a POST request, we would use the following code:

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://curlTest.com',
    CURLOPT_USERAGENT => 'Codular Sample cURL Request',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        item1 => 'value',
        item2 => 'value2'
    )
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

There you have a POST request that will work the same as our GET request above and return the response back to the script so that you can use it as you want. Errors As much as we all hate errors, you really need to take care to account for any eventuality with cURL as ultimately you will not have control over the site(s) that you are sending your request to, you cannot guarantee that the response will be in the format that you want, or that the site will even be available. There are a few functions that you can use to handle errors and these are: curl_error() – returns a string error message, will be blank ” if the request doesn’t fail. curl_errno() – which will return the cURL error number which you can then look up on this page listing error codes. An example usage would be:

if(!curl_exec($curl)){
    die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

You might want to look at using the setting CURLOPT_FAILONERROR as true if you want any HTTP response code greater than 400 to cause an error, instead of returning the page HTML. curl_exec($theEnd); cURL is a behemoth, and has many many possibilities. Some sites might only serve pages to some user agents, and when working with APIs, some might request you send a specific user agent, this is something to be aware of.

MS DHONI INTERVIEW

You would be forgiven for thinking that winning the World Cup was MS Dhoni’s finest moment. After all, that wonderfully free hit over mid-on for six – a stroke of majesty and triumph to close the deal – lifted a heavy burden from the shoulders of all India. The young were sick of hearing about the old, hearing tales of Kapil Dev’s famous band who had won the World Cup in England in 1983. India’s youth finally had its moment and, best of all, had it at home, in the city of Gavaskar and Tendulkar, and from the hand of a captain whose swashbuckling style had made him as much a darling in the grand homes of Kolkata as in the fishing villages of Kerala.

MS and me are off for dinner. He is wearing trainers, jeans sawn off at the knee, a T-shirt and a camouflage jacket. We turn left out of the team hotel and left again down an alleyway that leads us immediately into the central square of another provincial English town. After a third left and a stroll past Nando’s – he wants a break, he says – we enter another chicken-type diner that he says is pretty good. During this short walk, the Indian captain is asked to pose for 25 photographs. To the first family of five he says no, but adds that he will be back a little later and will happily answer their call then. Fifty metres later he relents. Of course, five means 25. “Rahul [Dravid] taught me how to say no politely,” he says, “but in the end, when it is only a few people, why not.”

In the diner, the manager’s face lights up. Dhoni hands him a signed Indian shirt. Waiters gather to look. The place is near empty. We sit at a small table and drink orange juice. He is an unbelievably good-looking man. A woman comes over with her daughter. She shoves her forward and Dhoni gives a nice smile for the ‘snap’. The woman cannot contain herself and, puce with embarrassment, asks if she can do the same. “No problem,” he says. The husband comes over too. You’ve probably got the picture by now. The chicken arrives.

“Winning the World Cup was very special because it meant so much to so many. One thing about our country that is constant is cricket. The smile it brought to people’s faces was the thing I shall always remember. It reminded me, reminded all of us, of our importance to the lives of the Indian people less lucky than we are.

“But personally, no. The proudest moment of my own life was the day I accepted my ranks at the Pipping Ceremony.” Your ranks? Pray explain. “From when I was a young boy, I read books on weapons and tanks and combat. I was so interested in the army. Then later, my profile as an Indian cricketer allowed me access to the real thing so I spent time with soldiers, learning about their lives and sacrifices. I had friends in the Special Forces and heard their stories of missions, the risks they take, the commitment they must have to succeed. I met the chief of the Army Staff and told him about my lifelong fascination with the military. I asked if it was possible to become involved myself. Incredible as it may seem, he took a recommendation through various channels that finished up with the president of the country. The result of all this is that I became an honorary lieutenant colonel in the Parachute Regiment.” The things we don’t know!

More of the army in a minute. “I love my country,” he says. “I tell my wife she is only the third most important thing after my country and my parents, in that order. The point is that while I am an Indian cricketer I will devote myself to that cause. Cricket is not everything, not by any means, but it is a large part of who I am. Therefore I want to play in all formats of the game and to play as much as possible because before long it will be over. Then I’ll focus more on the Army!”

Does his leadership have any ideological ambition, I wonder? “I believe in the process more than the result. If you are properly prepared, physically and mentally, committed to the task and fully engaged in the moment, then I have no problem with the outcome. It is not about 95 per cent at this level and with this much exposure, it is about 100 per cent. A dropped catch is fine if you were focused and in the moment. It becomes a problem to me if you are not. I live for the moment, not the past, not the future. People ask about my remaining ambitions. They are simple. I want to keep playing well and to win. I want the Indian people to be proud of us.”

*********************************************************

As Dhoni talks, many aspects of his character which are otherwise well concealed begin to emerge. He is a thoughtful and intelligent person. He is driven by a deep morality. He is honest, he is loyal. And he does not suffer fools. Perhaps above all, he is utterly single-minded.

Dhoni Hits the winning six  Credit : INDRANIL MUKHERJEE/AFP/Getty Images

Dhoni Hits the winning six Credits : INDRANIL MUKHERJEE/AFP/Getty Images

“My wife wants me to eat fish, she says it is delicious. But I don’t like fish, so that is that. I find liquor has a bitter taste, so I don’t drink alcohol but understand that other people enjoy it. I care most about how people live their lives, what choices they make and how they get the best from themselves. This is why people such as Amitabh Bachchan [Indian actor], Sachin and Rahul are heroes of mine. And I love young talent, Ajinkya Rahane for instance, and urge him to be himself, to trust his talent and allow it to work for him within the parameters of his capabilities. He did that at Lord’s and the hundred he made was among the best I have seen by any Indian batsman.”

Given Dravid is working for television on the series, I seek him out. “I cannot speak highly enough of MS. He has a unique ability to ignore consequences and soak up pressure. This makes it easier for everyone else. When he took over the captaincy it was immediately obvious that he held the seniors in great respect but was not overawed by them. He is calm and measured. Win, lose, he can walk away. I don’t know many, if any, who can retain their perspective like he can.”

Dhoni’s father was strict, which taught him respect – “and punctuality” – above all else. “When I was young I was closer to my mother but while I was in Class 12 (18 years old) the reason for my father’s reference to discipline became clear to me. Exams were due and I needed to study but the last day of preparation coincided with a big cricket match. I asked him what I should do. In turn, he asked if I was ready for the exam, which I felt I was. ‘With only 24 hours to go you will either be ready or you will not. Go and play,’ he said, ‘and have some fun for there is nothing more you can do in your studying.’ In other words, you now have your priorities in order.”

Dhoni and his beautiful wife, Sakshi Singh, live with his parents in Ranchi. True. The man listed on the Forbes 100 list as the 22nd richest sportsperson in the world lives with Mum and Dad. How cool is that. Twelve years ago he was working for the railways as a ticket collector. Now he could buy a jet plane. “There is some myth to the railway story. We are a middle-class family and my luck to have some talent as a sportsman meant I qualified for a Sports Quota (like a government scholarship). This gave me a proper job, for life had I wanted it, and it allowed me to earn some money, gain some life experience but also to focus on cricket. I thought I was reasonably good but didn’t think I would get this far. Every morning for four years, eight months a year, we practised at 6.30am – always with two new balls and very competitively. We were taught to bat for 20 minutes without getting out and then to challenge ourselves with game situations to which we had to adapt.

“When we were kids, our sport was dependent on weather. When the sun shone it was cricket. If not we played football. I was a goalkeeper but always played knockabout matches up front because I wanted to run around. When it rained we played table tennis and badminton. I just loved to compete. If I couldn’t, I wouldn’t play! Studies were important but sport was a passion.”

Back to the World Cup then and the innings that won it, the best one-day innings I have seen. Was it a massive gamble to promote himself above Yuvraj Singh, especially as he had relatively little batting during the tournament? “Something in me said I should go. I was changing into my batting shoes when Gary [Kirsten] came in and gave me a look. ‘Should I go?’ ‘Yes, I think you should go, man,’ he said. I had played a lot with Murali for Chennai in the IPL and felt I could read him. If I got it right, I felt I would calm the others down. You ask if it was a gamble. I believe that it is not about a right or wrong decision but about an honest and well thought through reason. I knew in my heart it was justified and felt that the team trusted me.”

He much enjoyed working with Gary Kirsten, who gave them all confidence. Their trust was implicit, as it is with Duncan Fletcher. “We are as one, it has to be that way. Gary had a fantastic skill to work with a player on a weakness without the player knowing it is a weakness. Indian people run on emotion and both he and Duncan have learnt to respond to that in their own way. Duncan is a little more old-fashioned than Gary and gets frustrated by things he sees as unprofessional but he is a very good man and there is more to him than meets the eye – humour for a start.”

*********************************************

We turn to life away from the game. He listens to Hindi music – “70’s and 80’s stuff mainly, it had more meaning to it then, the songs were deeper and more patriotic. I like reggae too, the beat works for me.” That passion for sport is matched by another: a devotion to his collection of 35 motorbikes.

“I love them all, expensive, cheap, old and new,” he says. A Harley? “Yes, a Harley Fat Boy. A Ducati Pantah, a Hellcat – one of the confederate motorcycles from the US, some ancient two-strokes, Triumphs, even an old BSA.” Does he ride them hard? “Not really. Well sometimes, but actually, I like to polish them. I’ll tell you a story. When I was a boy, one of our senior players used to position his beautiful bike behind me and then hit balls at me to catch. If I missed one and it hit the bike, I had to fix up the mark. Therefore I learned not to miss many. I’ve either been polishing bikes or trying to cling on to catches pretty much ever since!”

If you stop to think a while about the 33-year-old Mahendra Singh Dhoni, you can only marvel. This man leads the Indian cricket team under the greatest burden of expectation. He keeps wicket and bats in a key middle-order position in all three formats of the game. He has led India to all three ICC trophies and is a serial winner of the IPL with the Chennai Super Kings. He endorses 21 wide-ranging, high-profile products that include Pepsi, Reebok, Brylcreem, TVS Motors and Reliance. They say he is worth US$51 million. He has been awarded the fourth highest civilian honour in India, the Padma Shri, has been the ICC World Cricketer of the Year and is co-owner of the Mahi Racing team. All this, and, best of all, he likes to stay home and walk the dogs.

Conversation with Dhoni is stimulating. Unlike so many sportsmen, he avoids clichés. Better still, he engages. His interest is more in the person than the performance and his hope is to be remembered as a human being every bit as much as a cricketer.

The clarity of his perspective tells us much about the über-cool figure who finishes matches to such effect. “The problem for the finisher is that, too often, there is not much else to come. The big shot is not necessarily the best or smartest shot. What matters is staying in. I am never in a hurry. My aim is to leave it till the bowler and me are on level ground, i.e. to the point at which he is under as much pressure as me. Then we see who can handle it. And most crucially, to stay humble. Assume nothing. Each day is different. I always tell the team that today somebody will be a hero. You have the chance, go and take it.”

MS is an honorary lieutenant colonel in the Parachute Regiment.

MS is an honorary lieutenant colonel in the Parachute Regiment.

Perhaps there is a little military discipline in these performances. Perhaps the Army has rubbed off .
“Yes, maybe. I have made a real study of manoeuvres, of geography and physics, and of mental application to various challenges. I had the choice of four regiments but chose a fifth, the Parachutes, because it is a hundred per cent volunteer regiment. They are derived from the British SAS – an incredible unit, you know – and I felt I would be able to contribute more with them later in life. I believe I know enough to justify the honour and was worthy of it. One day I will train with them and I will jump with them too because I am keen to truly earn my wings.”

We move back to cricket and particularly to the nuances of the five-day game. He admits India were sub-standard in England three years ago but says that injuries to important players and a feel that the guard was changing contributed to that. “You cannot hide in Test cricket, England were too good for us. The challenge of Test cricket is unmatched by anything else in my experience. I love the planning and the strategy, the requirements of patience and consistency and the chance of recovery. But I get bored playing on a dead pitch. Trent Bridge was one of those, to play and to watch. Pitches should have their indigenous qualities exaggerated. Spin in India, pace and bounce in Australia, seam and swing in England. To survive in the modern era, the game needs results, not draws.” He is a fan of the 50- over game – “because it bridges the gap. Test cricket and T20 are too far apart; 50 overs is an important format”.

I ask if he has days, weeks, seasons even, when he is over it; if there are periods when he would rather be a million miles away. Surely the urge for escape must overtake his enthusiasm for game and country. “Not really. I don’t think about it much. Only injury brings on that sort of doubt or emotion. Otherwise, I live for the moment. I’m with you here and that’s good. And tomorrow will be good. Essentially I’m very content and happy with my life.

“Sakshi provides the spark I might sometimes need. Jokes aside, she is the person to whom I have reference. She is both loving and inspirational. Sometimes she travels with me but cricket is easier without having to ensure her safe passage. One day, we will see the sights together and she says that then, can she please move to number two on the list of my priorities! But for the moment I am here to play cricket. I have been given this honour to play for my country so I want see it through.”

The food is finished. Word has spread and fans are gathering at the diner’s door. We switch to short, sharp questions and equally short responses.

Will you grow your hair long again? “No.”

Why did you shave it off after the World Cup final? “It had been a stressful few months, I admit. Many people were demanding that we won the World Cup at home. The bare heard was the final goodbye to the pressure we had been under. It was like we could start afresh.”

If you could have one player from any other team, who would it be? “Any pacy, seam-bowling allrounder. You can build a team around one of those. And no, I’m not naming names!

****************************************

How long will you play for? “My job is to get the team right and then move away. I couldn’t stand down after the tour here three years ago. I don’t believe in deserting when the chips are down. And speed, I’ll know it is time to finish when I am no longer one of the fastest movers in the team. At that point I will surely know age is winning.”

Other than involvement with the military, what will happen when you retire? “I would like to build some academies, to provide facilities and opportunities for the young.”

Is it ever lonely on the road? “My door is always open, unless Sakshi is in town and then it shuts occasionally. Seriously, I always leave the door to my room open and there is a constant flow and buzz. No, I am never lonely.”

Does anything keep you awake at night? “No, I sleep well.” Oh come on MS, there must something, some anxiety? “Well, maybe if I am going shooting the next day, to the pistol range say, then the excitement has me tossing and turning a bit.” Ye gods.

Will you have children? “I hope so! We are practising!!” And what will you expect of them? “A sense of fun, some discipline, to treat others as they would treat themselves.” Sport? “If they would like. They will be raised with an outdoor life, running free and happy I hope.”

Is he planning to stay at home with Mr and Mrs D? “Why not? Our culture is different to yours. It is more European. We look after our parents when they get old as they looked after us when we were young.”

He gets up from the table to pay the bill. The waiters line up for selfies. Outside, the night is closing in but still people want their piece of history with the most marketable, perhaps most remarkable, cricketer on the planet. As we walk, I ask if he thinks captaincy is a calling. “Well, I remember playing in a senior district match, long before I ever captained a team. I was bowling and one of the team dropped a catch and then, with a couple of others, laughed about it like they couldn’t care less. Between overs I told them to stand together, well out of the way, and laugh all they like. I said the rest of us will get on with taking the wickets.

“I suppose that suggests I wanted to be the leader. Time allows you to learn to read the game and then you work out the direction you think a team should take. After that, you decide on the process and if you get the job, you put it in place. So we are back with the process, where we began two hours ago.”

Before we say goodnight, he leaves me with a simple final thought. “A perfect smile is too often missing from the faces of the world. If cricket can provide more smiles, it is doing its job.” He said we should meet again and that he would tell me more about his friends in the special forces. I can’t wait.

This interview featured in issue 119 of All Out Cricket magazine.

Source : http://www.alloutcricket.com/cricket/features/aoc-exclusive-ms-dhoni-interview

Disable an index in postgres

If you have got one index on a table that you would like to temporarily disable you can do following steps.

You can poke the system catalogue to disable an index:

update pg_index set indisvalid = false where indexrelid = 'test_pkey'::regclass

This means that the index won’t be used for queries but will still be updated. It’s one of the flags used for concurrent index building.

If you want to run a single query without using index, you can use following query.

begin;
drop index mytable_index;

explain analyze select * from mytable;
rollback;

I don’t think there is a way to disable just one, though you can do this in a transaction to make recovering from it very simple. You can also disable indexscan to disable all indices.

Reference : http://stackoverflow.com/questions/6146024/is-it-possible-to-temporarily-disable-an-index-in-postgres

‘ascii’ codec can’t encode characters : ordinal not in range(128)

I work on a system which fetches data from a database (Postgres) using python & writes them into a csv file. Few days ago i came through an error which was showing “ascii codec can’t encode characters : ordinal not in range(128)” whenever i tried to fetch data from a particular date range.

Problem : 

When ever i tried to fetch data of a particular data range (suppose date range between 1-Jan-2013 to 31-Jan-2013) error popped up. although data fetching between other data range was working fine. That clearly meant that problem was in data of that date range only. Actually problem was in few entry of one of the column(s) (email) of my table, which was storing some Non-ASCII value . When python tried to write that value into the csv, it couldn’t process the value & error came up.

Solution :

Either you can make that value (data) unicode in python by  using:

email = [DefectedValue].encode('utf-8').strip()

Or you can find the data actually causing problems and remove them. for finding the defected data you can run following command in your PostgreSQL terminal.

SELECT * FROM YourTableName WHERE EffectedColumn similar to '%[^\x20-\x7e]+%' ;

You can update/delete that tuple which is storing Non-ASCII characters.

Make primary key with two or more field in Django

Most of the time, people don’t actually need their composite (multi-column) key to be the primary key.

Django operates best with surrogate keys –  that is, it automatically defines an autoincrement field called id and sets that to be the primary key. That is suitable for almost all the users.

If you then need to enforce a unique constraint on your model across two or more fields, you can use the unique_together setting in the inner Meta class.

class Student(models.Model):
    Name = models.CharField(max_length=100)
    Roll = models.CharField(max_length=20)
    Department = models.CharField(max_length=15)

    class Meta:
        unique_together = (('Roll', 'Department'),)

It would act as a surrogate primary key column.

Sets of field names that, taken together, must be unique:

unique_together = (('Roll', 'Department'),)

This is a tuple of tuples that must be unique when considered together. It’s used in the Django admin and is enforced at the database level (i.e., the appropriate UNIQUE statements are included in the CREATE TABLE statement).

For convenience, unique_together can be a single tuple when dealing with a single set of fields :

unique_together = ('Roll', 'Department')