Tuesday 19 November 2013

Helpful Postgres Statements


 Find to 20 largest tables in the database


 SELECT nspname || '.' || relname AS "relation",
    pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
  FROM pg_class C
  LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
  WHERE nspname NOT IN ('pg_catalog', 'information_schema')
    AND C.relkind <> 'i'
    AND nspname !~ '^pg_toast'
  ORDER BY pg_total_relation_size(C.oid) DESC
  LIMIT 20;

Drop existing connections for a particular database

This will drop existing connections:
Query pg_stat_activity and get the pid values you want to kill and issue select pg_terminate_backend(pid int) to them.

PostgreSQL 9.1 and below:

SELECT pg_terminate_backend(pg_stat_activity.procpid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB';

PostgreSQL 9.2 and above:

SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB';

Creating a user to access a server

The idea is to create a user and assign the user with permissions to access a single database from a particular IP source. They will be able to access on the port set out.
  • set up user in postgres (using super user account)
CREATE USER user234 WITH PASSWORD '3f5ffg#' VALID UNTIL '2014-04-01';
GRANT CONNECT ON DATABASE mydb TO user234;
GRANT SELECT ON table23 TO user234;
GRANT SELECT ON table24 TO user234;
GRANT SELECT ON table25 TO user234;
GRANT SELECT ON ALL TABLES TO user234; -- bah.. just give permission to all 
  • edit # vi /var/lib/pgsql/9.1/data/pg_hba.conf
host    mydb   user234            172.23.20.32/24         md5
  • Reload conf as postgres user
/usr/pgsql-9.1/bin/pg_ctl reload -D /var/lib/pgsql/9.1/data/

Datetime Setting

The problem lies because of the date/time field settings..

to fix this we can first show the datestyle in the database..

=> SHOW datestyle;
 DateStyle
-----------
 ISO, YMD
(1 row)

Then we can set the datestyle to the correct style..


=> SET datestyle = "ISO, DMY";
SET
=> SHOW datestyle;
 DateStyle
-----------
 ISO, DMY
(1 row)

Restart Postgres

/etc/init.d/postgresql restart

OR


service postgresql restart

Set the timezone in a PSQL DB

=> SET TIME ZONE 'Australia/Victoria';
SET
=> SHOW TIME ZONE;
      TimeZone
--------------------
 Australia/Victoria
(1 row)

PSQL commands to file

psql -U user234 -d database -H -c "select * from ...;" -o /home/me/file.html

Database Backup

Simple backup which is compressed.

pg_dump -U user234 mydb | gzip > /home/me/backups/mydb_YYYMMDD.sql.gz

exclude a table


pg_dump -U user234 --exclude-table=tabletoexclude mydb | gzip > /home/me/backups/mydb_YYYMMDD.sql.gz

Schema Only

pg_dump -U user234 --schema-only -f mydb_backup_schema_YYYMMDD.sql mydb

Database Restore

Restore from the backup made

psql -U postgres
DROP DATABASE mydb; CREATE DATABASE mydb OWNER user234;\q
gunzip -c /home/me/backups/mydb_YYYMMDD.sql.gz | psql -U user234 mydb;

Database Clone

Clone the database, an easy way to set up a clone to test with.

CREATE DATABASE newdb2 WITH TEMPLATE mydb;

The following will drop the database and copy back the original one. This doesn't really follow with the previous statements, so read carefully.

DROP DATABASE mydb;
CREATE DATABASE mydb WITH TEMPLATE mydb_original;
ALTER DATABASE mydb OWNER TO user234;





Thursday 7 November 2013

Downgrading / Switching openJDK and other Java versions in Ubuntu

If you are running openJDK or other versions and need to develop or use Java in an older version there is an easy way to switch working versions.

First confirm your current version;

root# java -version 
java version "1.7.0_25" 
OpenJDK Runtime Environment (IcedTea 2.3.10) (7u25-2.3.10-1ubuntu0.13.04.2) 
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode) 

Note: This way assumes that you have upgraded but not removed the older version. If you only have the newer version then first install the older version you want to downgrade to then you can simply switch.

The command you should run is:

root# update-alternatives --config java 

Upon running you will receive a similar screen and switching to another version is as easy as typing a number.

There are 3 choices for the alternative java (providing /usr/bin/java). 

Selection Path Priority Status 
------------------------------------------------------------ 
* 0 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1071 auto mode
 1 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 manual mode
 2 /usr/lib/jvm/java-6-sun/jre/bin/java 63 manual mode
 3 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1071 manual mode

Press enter to keep the current choice[*], or type selection number: 1 
update-alternatives: using /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java to provide /usr/bin/java (java) in manual mode 

You can then check your version again;

root# java -version 
java version "1.6.0_27" 
OpenJDK Runtime Environment (IcedTea6 1.12.6) (6b27-1.12.6-1ubuntu0.13.04.2) 
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)