Tuesday, February 2, 2010

An Update on Prompts

Well, the prior prompt had a problem.

The main symptom was that the command line would wrap before reaching the right edge of the terminal window (which is annoying but not terrible), but in wrapping, it would only issue the carriage return, not the line feed, so that the rest of the line was commingled with the prompt and the beginning of the line. That was beyond annoying. Google Fu 'os x terminal prompt mac \e[ confuses' found this page, which linked to this one which explained the problem.

In short \e opens an escape sequence, but does not close it. The fix is to replace \e with \[\033 and close the entirety with \]. That leaves a remarkably unreadable definition of PS1:

PS1='\[\033]2;\u@\h:\w\a\[\033[36;40m\]\u@\h:\w \$ \[\033[0m\]'

I've also noticed that logging in from a linux virtual terminal in text mode doesn't handle the system escape to set the title bar — \[\33]2 — very well. I've added a conditional that checks SSH_TTY, and that seems to work, but I know there are probably more cases that I haven't considered.

One last thing, for some reason, no matter what I do, su does not read .bashrc. I've tried su -, su -l, creating a .bash_login. I'm stumped.

Bringing Up Nuj

I moved over some of the supporting scripts, added environment variables to my .bashrc, and updated my path to include PostgreSQL. While attempting to create a superuser for the Django instance, I discovered I needed to install psycopg2, the Python-PostgreSQL interface library.

After much round and round, I found the magic formula*:

pat@stoat:~ $ sudo easy_install -U setuptools
password:
pat@stoat:~ $ export PATH="$PATH:/Library/PostgreSQL/8.4/bin"
pat@stoat:~ $ sudo easy_install psycogp2

There were a lot of 'defined but not used' warnings, but it succeeded. I haven't tested it yet. I guess that's next.

*A prerequisite is Xcode, which I downloaded from Apple. It's not part of the formula, per se, since I suppose most folks will probably already have it installed. (I would have, were I not working with a new hard-drive and OS install.)

Useful links:

Monday, February 1, 2010

Improving the Command Prompt

I'm used to a more information rich prompt than is the default with bash on OS X.

I found a helpful page by Daniel Robbins, President and CEO of Gentoo Technologies.

Current prompt is now

PS1='\e]2;\u@\h:\w\a\e[36;40m\u@\h:\w \$ \e[0m'

which sets the current user, host, and full path into the titlebar of the window, and which sets the prompt to the same, but in cyan.

Mac OS X PostgreSQL Database Setup

The PostgreSQL documentation is quite good.

Section 1.3 Creating a Database suggests first trying

$ createdb mydb
-bash: createdb: command not found

Looking around, I find /Library/PostreSQL/8.4/bin/createdb and

/Library/PostgreSQL/8.4/bin $ ./createdb mydb
Password:
createdb: could not connect to database postgres: FATAL: password authentication failed for user "pat"

Pretty much everything I tried ended with that. Whether as user root or postgresl, always the request for a password. On setup, I used the same password as I use for PostgreSQL on the web host. A little Googling and forum surfing led me to instructions for handling this very situation. I needed to edit the configuration file for host-based authentication:

/Library/PostgreSQL/8.4/ $ emacs data/pg_hba.conf

I changed the line

local all all md5

to

local all all ident

to allow me to use the utilities as the user postgres without entering a password. Then

/Library/PostgreSQL/8.4/ $ kill -HUP <PID of /Library/PostgreSQL/8.4/bin/postmaster>
/Library/PostgreSQL/8.4/ $ bin/createuser pat
Shall the new role be a superuser? (y/n) y
/Library/PostgreSQL/8.4/ $ bin/psql
psql (8.4.2)
Type "help" for help.

postgres=# alter role pat password 'redacted';
ALTER ROLE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+-------------+-----------
pat | Superuser | {}
: Create role
: Create DB
postgres | Superuser | {}
: Create role
: Create DB

postgres=# ^D
/Library/PostgreSQL/8.4/ $ bin/createdb pat

I then reversed the edit of data/pg_hba.conf and sent HUP to the postmaster again. Back in my own account,

pat@stoat:/Library/PostgreSQL/8.4 $ bin/psql -U pat -W
Password for user pat:
psql (8.4.2)
Type "help" for help.

pat=# \du
List of roles
Role name | Attributes | Member of
-----------+-------------+-----------
pat | Superuser | {}
: Create role
: Create DB
postgres | Superuser | {}
: Create role
: Create DB

pat=#

Next I'll handle Nuj-specific db items.