Friday, July 24, 2009

Windows SYSTEM user

I have a program that copies files from one machine to another over a network. It uses port 445 (called the SMB port - SMB is a protocol for sharing files between computers in a network).

When I ran this program in MS DOS, the remote folder in which the remote files are in was recognised as a directory (i.e. dir.isDirectory() = true). However, when I ran this program from Windows Services, the remote folder was not recognised as a directory.

The reason for this was because of the user under which the program is ran. When it was run in MS DOS, the current Windows user (Administrator) was running the program. Hence, it had access priviledges to read/write the remote folder.

However, as the Windows Services process is controlled by System User, when the program was run in Windows Services, the program was run under the SYSTEM user, disallowing it to have access priviledges over the remote folder.

Basically, although a SYSTEM user is higher than an administrator and has full control of the OS and kernel, it cannot access remote systems. It is just for a local system.

To ensure that the program is run under the current Windows user (Administrator), you will need to set up the program's properties in services.msc. Click on the Properties of the program, then on the 'Log On' tab. Here, ensure that you log on as 'This Account' (type in your current Windows Administrator username and password) and NOT as 'Local System Account'.

This will allow the program (running as a Windows Service) to copy files over a network as it will be running under the current user. You can check the username it is running under by going to your Task Manager.

Thursday, July 23, 2009

Basic PostgreSQL Must-Know's

1. String concatenation using the PL/pgSQL language is done like this:

declare
split_table_name text[];
v_PK text;
v_Query text;

v_Query := 'select ' || v_PK || ' from ' || split_table_name[1];


2. To run the above query string variable (i.e v_Query), use the EXECUTE statement like this:

declare
result text;

EXECUTE v_Query into max_num_str ;

or

EXECUTE 'select ' || v_PK || ' from ' || split_table_name[1] into max_num_str ;

Advanced PostgreSQL Queries

All Postgres users, like it or not, will need to retrieve information about the tables, triggers, sequences etc that they have in their databases. The following are some of the things you might want to query:

1. What constraints does my table have?


select pgc.relname as "TableName", pc.conname as "ConstraintName",
contype as "ConstraintType", conkey as "KeyCols",
confkey as "ForeignCols", consrc as "Src"
from pg_class pgc, pg_constraint pc
where pgc.oid = pc.conrelid
and relname = '< tablename >';


2. How to get the column name of the primary key of a table?

select column_name from information_schema.key_column_usage where constraint_name IN
(
select c.conname as "ConstraintName"
from pg_class r, pg_constraint c
where r.oid = c.conrelid
and contype = 'p'
and relname = '< tablename >' );


3. What triggers are on my table?

select pc.relname as "Table", pt.tgname as "TriggerName",
pt.tgconstrname as "ConstraintName", pt.tgenabled as "Enabled",
pt.tgisconstraint as "IsConstraint", pcc.relname as "ReferencedTable",
pp.proname as "FunctionName"
from pg_trigger pt, pg_class pc, pg_class pcc, pg_proc pp
where pt.tgfoid = pp.oid and pt.tgrelid = pc.oid
and pt.tgconstrrelid = pcc.oid
and pc.relname = '< tablename >';


4. What indexes are on my table?


select * from pg_indexes where tablename = '< tablename >';


More Advanced PostgreSQL Queries can be found here:
http://www.thegeekstuff.com/2009/05/15-advanced-postgresql-commands-with-examples/

Monday, July 13, 2009

Setting the 'Show Desktop' keyboard shortcut in Ubuntu

Having just moved from a Windows environment to Linux, I have been trying to find my way around Ubuntu 9. Everytime I have something that I want to do in the OS, I have used the Windows way of doing it, without any success :p

For those of you trying to set up your Linux OS to 'Show Desktop' when the Windows + D key is pressed, this is what I found out on how to do it:
1. Go to System -> Preferences -> Keyboard Shortcuts

2. By default, Ubuntu responds to Ctrl-Alt-D to 'Show Desktop'. You can change this to Windows + D by firstly pressing the Windows key (or called Super in Linux which I havent quite figured out why), then simultaneously clicking on the keyboard shortcut for the "Hide all normal windows and set focus to the desktop background" option and hitting the 'D' key.

This will map a Windows-D keypress to 'Show Desktop'!