PostgreSQL: Difference between revisions

From Wildsong
Jump to navigationJump to search
Brian Wilson (talk | contribs)
Brian Wilson (talk | contribs)
Line 34: Line 34:
  Wood Canyon
  Wood Canyon
  (10 rows)
  (10 rows)
'''SELECT COUNT(*) FROM rivers WHERE name LIKE '% River''''
  count
-------
  312
(1 row)


''More handy queries'' (Courtesy PostgreSQL 9 Admin. Cookbook, Packt Pubs)
''More handy queries'' (Courtesy PostgreSQL 9 Admin. Cookbook, Packt Pubs)

Revision as of 20:10, 30 October 2011

Standard admin tool

I use pgadmin3 version 1.12 because it is available on every platform and works the same everywhere and it is free. The same client works with both 8.4 and 9.0 postgresql.

Command lines tools

psql gives you command access to PostgreSQL. Normally I set my system to trust local connections in the pg_hba.conf file then use psql -U postgres.

There are special psql commands starting with '\' so do '\?' to list them. Typically I use psql when I want to answer a few questions quickly. I also use it with pipes to load data. See the section on shp2psql in the PostGIS page.

Q. What databases are present on this server? \l or \l+ Adding the' '+' to any command includes extra information, such as the amount of space used.

Q. What tables are present in a database? \d or \d+

Q. What columns are present in a table? \dS TABLENAME

Q. How do I do a query? SQL works, so once I know a database and a table I can do regular SQL queries

\c crbc_spatial;
SELECT name FROM rivers WHERE name IS NOT NULL LIMIT 10;
          name           
--------------------------
Rock Creek
Long Meadow Creek
Weed Lake
Frenchman Creek
St. Martin Creek
South Fork Tenmile Creek
Sunflower Canyon
Chicken Springs Canyon
Cottonwood Canyon
Wood Canyon
(10 rows)
SELECT COUNT(*) FROM rivers WHERE name LIKE '% River'
 count 
-------
  312
(1 row)

More handy queries (Courtesy PostgreSQL 9 Admin. Cookbook, Packt Pubs)

SELECT current_database();
SELECT current_user;
SELECT version();

Associated tools

PostGIS - spatially enabled PostgreSQL

SpatialKit - a connector to use PostGIS from ArcGIS.

Navicat - a GUI front end for PostgreSQL. See http://www.navicat.com/ Includes report generation tools

MS Access

I don't actually use Access and don't currently have a copy installed anywhere so someone should write up and add comments to this section.

article on using MS Access with PostgreSQL

The crucial aspect here is to install the PostgreSQL ODBC driver on Windows.

Things to note and perhaps deal with

  1. Access users don't expect case-sensitivity.
  2. There is no support for booleans in Access so you have to hack around this

Openoffice / Libreoffice Base

Use the SDBC driver. On Ubuntu this means selecting the package in Synaptic and installing it. libreoffice-sdbc-postgresql in my case. On Ubunu Base is not installed by default so I also installed libreoffice-base

The Datasource URL requested in Base should look like this:

host=myhostname.wildsong.biz port=5433 dbname=mydatabase

I have to know my username and password to test the connection. I have to tell it the name of a file to save the settings information in on the local computer.

Most critically at this point I have to know how to use both Base and PostgreSQL. :-)

= Use case: Loading a spreadsheet into PostgreSQL

I have received a spreadsheet for the Mapping Vietnam project.

It has a GRID COORD column that must be converted to a point geometry, and it has a bunch of attribute columns.