Admin Pack for PostgreSQL and pgAdmin

PgAdmin3 admittedly is a nice Postgres GUI. It's got features from simple query tool to server monitoring. However, if you have your "Guru Hints" on, you'll see the following message in a pop-up window:

Server instrumentation
The server lacks instrumentation functions.

pgAdmin III uses some support functions that are not available by default in all PostgreSQL versions. These enable some tasks that make life easier when dealing with log files and configuration files.

When you install PostgreSQL 8.0 or up using the Windows installer, you just need to select the "admin" or "adminpack" module.c

When compiling from source, the necessary files can be found in the xtra subdirectory of the pgAdmin source tree. For PostgreSQL 8.0, copy the admin directory under the postgresql contrib source directory, make and make install from there. For PostgreSQL 8.1, use the admin81 directory for that.

PostgreSQL 8.2 and above include the instrumentation functions in the "adminpack" contrib module. After the module is installed, you need to create the instrumentation functions in your maintenance database using the admin.sql script (admin81.sql for PostgreSQL 8.1) which are usually located in the pgsql share directory (e.g. /usr/local/pgsql/share)

Extended server instrumentation is not supported for PostgreSQL 7.3 and 7.4.

While this does provide useful informaiton about some sort of adminpack that is necessary, the actually steps necessary are omitted.


What is adminpack?

As it turns out Postgres comes with a number of additional helpful "contrib" sets of functions which you can install on demand .

One of these sets of function is the Admin Pack it, which:

"provides a number of support functions which pgAdmin and other administration and management tools can use to provide additional functionality, such as remote management of server log files."

Read more about it at http://www.postgresql.org/docs/8.4/static/adminpack.html


Installation:

Locate adminpack.sql on your system.
On Gentoo for PostgreSQL 8.4 it is in: /usr/share/postgresql-8.4/contrib/

Installation itself is quite simple:

psql -U postgres --file /usr/share/postgresql-8.4/contrib/adminpack.sql
* Substitute "postgres" for your distor's default PosgreSQL admin user.

Now close down pgadmin, restart PostgreSQL server and run pgadmin again.

If you ever need to uninstall it you can run:

psql -U postgres --file /usr/share/postgresql-8.4/contrib/uninstall_adminpack.sql



Other Utility Functions:

AdminPack is not the only goodie that Postgre comes with. The full list of additional utilities can be found here: http://www.postgresql.org/docs/8.4/static/contrib.html

A quick look inside the contrib/ folder reveals:

_int.sql            fuzzystrmatch.sql        pg_trgm.sql
adminpack.sql       hstore.sql               pgcrypto.sql
autoinc.sql         insert_username.sql      pgrowlocks.sql
btree_gin.sql       int_aggregate.sql        pgstattuple.sql
btree_gist.sql      isn.sql                  pgxml.sql
chkpass.sql         lo.sql                   refint.sql
citext.sql          ltree.sql                seg.sql
cube.sql            moddatetime.sql          sslinfo.sql
dblink.sql          pageinspect.sql          tablefunc.sql
dict_int.sql        pg_buffercache.sql       test_parser.sql
dict_xsyn.sql       pg_freespacemap.sql      timetravel.sql
earthdistance.sql   pg_stat_statements.sql   tsearch2.sql

Configuring PostgreSQL to use Database Stored Passwords

One of the neat features of PostgreSQL is it's flexible authentication. It allows use to map system logins to specific postgres users using pg_ident.conf and configure specific trust levels for any users, ip ranges/sockets, and databases you want in pg_hba.conf

However, what sometimes confuses a newcomer to Postgres (and in my experience MySQL folks) is that on on a number of distros the default installation does not really require them to enter the password when logining into "psql" or any other DB GUI. In fact many people continue to provide the password not knowing it is not actually having any effect.

Please note that I use Gentoo as a example disto, but only affects the defaults, such as file location and usename choices.

This happens because the default configuration of Postgres to automatically authenticate all localhost users. This is how it is done.

Locate and open Postgres pg_hba.conf file. (On Gentoo, it is in "/var/lib/postgresql/8.4/data/" where 8.4 is the current Postgres version.

Look at he following lines:

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
# "local" is for Unix domain socket connections only
local   all         all                               trust
# IPv4 local connections:
host    all         all         127.0.0.1/32          trust
# IPv6 local connections:
host    all         all         ::1/128               trust

As you can see the authentication method for local access, for all uses and all database and local IPs is set to "trust" which means they will be logged in without any password.

Postgres comes with a large variety of authentication methods such as:
"trust", "reject", "md5", "password", "gss", "sspi", "krb5", "ident", "pam", "ldap" or "cert".  Note that "password" sends passwords in clear text; "md5" is preferred since it sends encrypted passwords.

STOP: If you change your method to "md5" right now and reload Postgres, you'll probably be stuck with no way to get in to the database. I suggest you first change the default admin password. On Gentoo the admin user is "postgres" though on FreeBSD and some other systems it is different.

Keep the method at "trust" for now.

Now login to psql" from console.

psql -U postgres
* Change the Postgre admin "username" according to your system.

On Gentoo, your user has to belong to "postgres" group to be able to login to postgres using a Unix socket. Alternatively you can use sudo or specify the localhost using -h. (Example: psql -U postgres -h localhost). This probably works on other distributions.

ALTER USER postgres WITH LOGIN ENCRYPTED PASSWORD 'yourpassword';
* Change the Postgre admin "username" and "yourpassword" according to your system.

As you might have guessed the "LOGIN" option makes sure the user can do interactive logins.

Now edit pg_hba.conf and replace "trust" with "md5" and reload Postgres.

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
# "local" is for Unix domain socket connections only
local   all         all                               md5
# IPv4 local connections:
host    all         all         127.0.0.1/32          md5
# IPv6 local connections:
host    all         all         ::1/128               md5


All the options for configuring pg_hba.conf and Postgres authentication are available here:  http://www.postgresql.org/docs/8.4/static/auth-pg-hba-conf.html

INTERESTING NOTE:

By default Postgres users *are NOT* per-database but are instead server users. However that should not be confused with user-database permissions, as Postgres fully supports those.

If you want to look into configuring Postgres with per-database users look please read about "db_user_namespace" server options - http://www.postgresql.org/docs/8.4/interactive/runtime-config-connection.html

Volume Control - OSD style

I've always liked the big giant volume control OSD that Gnome had (before Ubuntu changed it notification style).  However if you do not use gnome, you are a bit out of luck. After some searching I found a small utility written in PyGTK by someone having the same problem.

It's run from command line, pop-ups a window, and quits after 2 seconds;

It was a bit too small in size for my taste, but easily corrected. So with couple of small modifications, it now looks a bit closer to what I like in Gnome.


Usage: pvol.py [-s] [-m] [-c PERCENT] [-p] [-q]
Options:
  -h, --help            show this help message and exit
  -s, --status          display current volume
  -m, --mute            mute the main audio channel
  -c PERCENT, --change=PERCENT
                        increase or decrease volume by given
                        percentage
  -p, --pcm             change PCM channel (default is MASTER)
  -q, --quiet           adjust volume without the progressbar

Following bindings work pretty well for me:
Mute        : ./pvol --mute
Volume Up   : ./pvol --change=10
Volume Down : ./pvol --change=-10


This modified version is available here for download.

Useful Compiz Plugins: Vigo, PutPlus, SmartPut, Toggle-Decorations

IMHO, Compiz window manager is configurable enough for keyboard use if used with certain plug-ins.

Here are some plug-ins that I find essential:
  1. Vigo -  Switch beween windows using arrow keys.
  2. PutPlus, SmartPut - quickly positioning using a window to specific places on your screen, and moving them around.
  3. General  Options - Toggle Horizontal, Vertical, and Full Screen Maximize.
  4. Viewport Switcher - bind "Go To" for each viewport to a key and switch on the fly.
  5. Scale Window Title Filter - run "Scale" and filter windows by typing their names.
  6. Group and Tab - save desktop space by grouping the window and flip them around.
  7. Toggle Decoration - Get rid of title bar to get more pixels of screen space.
  8. Negative - easily highlight your widows by inverting them

    Majority of the plug-ins are available in the regular compiz packages:
    • compiz-plugins-extra
    • compiz-plugins-main
    • compiz-plugins-unsupported

    Some, however, are still in dev land.

    I wrote a small script to install Vigo, PutPlus, SmartPut, and Toggle Decoration. It requires git, since the plugins are pulled from repositories. It has been tested with Gentoo and Ubuntu.


    To the plug-ins running quickly, install git using your package manager then run the following:

    cd ~;
    mkdir -p ~/tmp/compiz-plugins;
    cd ~/tmp/compiz-plugins;
    rm deploy-plugins;
    wget -nc https://bitbucket.org/devnotes/random.dev.notes/raw/tip/compiz/deploy-plugins;
    chmod u+x  deploy-plugins;
    ./deploy-plugins;
    echo "Done"

    Meld is the best diffing tool

    Part of developer's life is about comparing changes made to the code.

    To that end, I've tried a number of visual diff-ing tools, ediff, vimdiff, kdiff3, kompare but none beats Meld.

     Usability is where Meld trumps the competition:


    • Use of curved lines and color highlighting to show different types of changes.
    • Works with pretty much every source control system there is.
    • Does directory diffs really well.
    • Has both 2-way and 3-way compares.
    • Has syntax highlighting, line numbers.
    • Works from command line.
    • ... and more
    Here are the two ways I use meld all the time:

    To see changes you made to your code base:

    meld .

    To see changes between checkout subversion trunk/ and branch/

    meld path_to/trunk path_to/branch

    You can also use meld as a parameter to --diff-cmd for your source control client.


    With either fink or macports you can get it on Mac.



    Cleaning out distfiles on Gentoo

    One thing about Gentoo is that it takes a bit of hard disk space to run it. For example the distfiles of outdated ebuilds do not automatically get cleaned out.

    But doing so is a breeze with eclean utility from app-portage/gentoolkit

    To clean (adding -i will prompt you before deletion, so it is also a dry run):
    eclean -i distfiles


    There are any number of ways you can do automatic cleaning, a few that come to mind are:
    • adding cron task
    • /etc/portage/postsync.d to clean out after every sync

    Cleaning up package.keywords and more ...

    If you are a power Gentoo user your package.keywords tends to get a bit full. Same is true of package.mask, package.unmask, package.use.

    What's worse is that after a while the version program your wanted goes stable you have to remember the remove the reference to it in one of the /etc/portage/package.* files or potentially spend more time resolving dependencies.

    Fortunately there is an excellent solution available in the form of eix-test-obsolete command. I'll let its help page describe itself:

    It calls eix -tTc several times with various variable settings in order to
    display missing packages or packages with obsolete entries in
    /etc/portage/package.*

    Eix is the uberfast, pre-cached Portage search alternative to emerge -s that every Gentoo system should have.

    emerge -av app-portage/eix