Flapping Head » Postgresql http://scottbarnham.com/blog Code and comments on web development, Django, Python and things (un)related. Wed, 22 Jun 2016 04:22:53 +0000 en-US hourly 1 http://wordpress.org/?v=4.3 Make a slug in PostgreSQL translating diacritics http://scottbarnham.com/blog/2010/12/20/make-a-slug-in-postgresql-translating-diacritics/ http://scottbarnham.com/blog/2010/12/20/make-a-slug-in-postgresql-translating-diacritics/#comments Mon, 20 Dec 2010 20:09:57 +0000 http://scottbarnham.com/blog/?p=79 I needed to make slugs in Postgresql from names which included diacritics. Though not complete, here’s a reasonable stab at it:

select regexp_replace(translate(replace(lower(your_field_name_here), ' ', '-'), 'áàâãäåāăąÁÂÃÄÅĀĂĄèééêëēĕėęěĒĔĖĘĚìíîïìĩīĭÌÍÎÏÌĨĪĬóôõöōŏőÒÓÔÕÖŌŎŐùúûüũūŭůÙÚÛÜŨŪŬŮ','aaaaaaaaaaaaaaaaaeeeeeeeeeeeeeeeiiiiiiiiiiiiiiiiooooooooooooooouuuuuuuuuuuuuuuu'), '[^\w -]', '', 'g') as slug

The translate is not quite right because it translates some uppercase to lowercase, but it’s all lowercase for the slug, so it doesn’t matter here.

[Update] Added ‘g’ flag to regexp_replace to replace all occurrences, not just first.

]]>
http://scottbarnham.com/blog/2010/12/20/make-a-slug-in-postgresql-translating-diacritics/feed/ 1
Migrating Postgresql Databases the Easy Way http://scottbarnham.com/blog/2009/06/23/migrating-postgresql-databases-the-easy-way/ http://scottbarnham.com/blog/2009/06/23/migrating-postgresql-databases-the-easy-way/#comments Tue, 23 Jun 2009 10:45:52 +0000 http://scottbarnham.com/blog/2009/06/23/migrating-postgresql-databases-the-easy-way/ When you upgrade Postgresl to a new major version (e.g. 8.1 to 8.3), all databases need to be dumped from the old version and loaded in to the new version. It’s not difficult, but on Debian there’s a really easy way.

Debian has pg_createcluster, pg_dropcluster and pg_upgradecluster (plus a few others). The one I’m referring to here is pg_upgradecluster.

It takes the version and cluster name of the databases you want to upgrade.

e.g. if you’ve installed Postgresql 8.3 and have databases in 8.1, just run:

# pg_upgradecluster 8.1 main

This upgrades the databases in the “main” cluster under version 8.1 and puts them under “main” in version 8.3. If you already have a “main” cluster in 8.3, you’ll need to drop it first.

This little tool not only dumps and loads the database, but it also changes the config so 8.3 runs on the standard port previously used by 8.1 (or whatever your older version). A painless way to upgrade.

]]>
http://scottbarnham.com/blog/2009/06/23/migrating-postgresql-databases-the-easy-way/feed/ 0