Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
postgresql [2008/07/14 16:05] a created |
postgresql [2009/06/12 01:27] (current) a terminate a query which is taking a long time to run |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Postgresql ====== | ||
+ | |||
+ | ===== General ===== | ||
+ | |||
+ | **Add pgplsql support** | ||
+ | % createlang plpgsql template1 | ||
+ | |||
+ | **Adding a column to a table** | ||
+ | alter table pfo_survey_response_2006 add column section text | ||
+ | |||
+ | **Backing up a database** | ||
+ | % pg_dump databasename > db.out | ||
+ | |||
+ | **Connecting to a user / database** \\ | ||
+ | Say you have your primary database user as ' | ||
+ | |||
+ | % psql -U nsadmin opeancs | ||
+ | |||
+ | **Creating a new database** | ||
+ | % createdb database-name | ||
+ | |||
+ | **Creating a new user** | ||
+ | | ||
+ | |||
+ | You can also do this for a passwordless user: % crateuser bork | ||
+ | |||
+ | **Creating a new user** | ||
+ | |||
+ | | ||
+ | |||
+ | **creating a new database** | ||
+ | |||
+ | | ||
+ | |||
+ | **changing postgsql user password** \\ | ||
+ | |||
+ | as postgres user (su - postgres) do: | ||
+ | < | ||
+ | $ psql template1 | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | \h for help with SQL commands | ||
+ | \? for help on internal slash commands | ||
+ | \g or terminate with semicolon to execute query | ||
+ | \q to quit | ||
+ | |||
+ | template1=# ALTER USER < | ||
+ | </ | ||
+ | |||
+ | **changeing ownership of database** | ||
+ | |||
+ | | ||
+ | |||
+ | **Inserting a time column** \\ | ||
+ | If you have the minutes, seconds, and am/pm and want to get them into a time column (time without timezone), you can do something like this: | ||
+ | psql=# select to_timestamp(' | ||
+ | to_timestamp | ||
+ | | ||
+ | 10:45:00 | ||
+ | (1 row) | ||
+ | |||
+ | **Limiting selections to X number of rows** | ||
+ | use the " | ||
+ | | ||
+ | where stuff.blah > 12 | ||
+ | limit 5 | ||
+ | |||
+ | **Logging in as a specific user** | ||
+ | % psql database-name user-name | ||
+ | |||
+ | **Logging into a remote database** \\ | ||
+ | On machine tower, dbname borkdb, user nettest | ||
+ | % psql -h tower borkdb nettest | ||
+ | |||
+ | **Manually starting the database** | ||
+ | % pg_ctl -D / | ||
+ | |||
+ | **Next value from a sequence** | ||
+ | | ||
+ | values (nextval(' | ||
+ | |||
+ | **Restoring a Database** | ||
+ | % psql -d databasename -f db.out | ||
+ | |||
+ | **Seeing available databases** | ||
+ | % psql -l | ||
+ | (lower case Ell) | ||
+ | |||
+ | **Selecting a random row from a table** \\ | ||
+ | use the random() function. | ||
+ | |||
+ | | ||
+ | (select src, width, height, | ||
+ | | ||
+ | from bw_eyes | ||
+ | order by 4) stuff | ||
+ | limit 1 | ||
+ | |||
+ | This selects a random row from bw_eyes and returns the interesting data from it. This query is used on the quickies pages to randomly select an image of eyes staring back. | ||
+ | |||
+ | **Time interval math to integer** | ||
+ | If you take the difference between two timestamps, you can end up with an unweildly value like " | ||
+ | | ||
+ | |||
+ | **Turning off the psql pager** | ||
+ | | ||
+ | Pager usage is off. | ||
+ | |||
+ | **pg interval math** | ||
+ | Older versions of pg would support select blah from whatever where date-column > (current_timestamp - 21) to get all the blahs from whatever in the last 21 days. This doesn' | ||
+ | |||
+ | | ||
+ | from blog | ||
+ | where whenx > (current_timestamp - interval '21 days') | ||
+ | order by whenx desc | ||
+ | |||
+ | **terminate a query which is taking a long time to run** | ||
+ | | ||
+ | FROM pg_stat_activity; | ||
+ | kill -INT procpid | ||
+ | |||
+ | |||
===== PostgreSQL Check Constraint Supports Regular Expressions ===== | ===== PostgreSQL Check Constraint Supports Regular Expressions ===== | ||
| |