PGIS-04 13: Postgres Nuts and Bolts
Postgres Nuts and Bolts
This module explores the building blocks of a Postgres database in greater detail.
Documentation
Documentation for all supported versions of PostgreSQL is available from https://www.postgresql.org/docs/ . There are subtle differences between how different versions are implemented, so it is advisable to use the appropriate version for the database you are working with. Within the documentation is a comprehensive reference for all SQL commands including a synopsis, description, parameter list, anticipated output, and use examples.
Data Hierarchy
Schemas are the equivalent of folders in a file system, supporting the organisation of other database objects into logical groups (for example planning, education, highways). All tables must be inside a schema, and schemas also hold function and views.
Within a schema, tables store the records or rows which make up your data, and have rows and columns like a spreadsheet.
Tables have columns which define the structure of the data stored in a table, including name, datatype (string, integer, date etc.) and size.
Create schema and table
Create a new schema in pgAdmin by navigating in your database to Schemas, then right-click > Create > Schema…
Give the schema a name and keep the owner as postgis, then click Save to create it
Now recreate the structure of an existing table in the new schema
Navigate to crime > neighbourhood_crime, and click SQL in the toolbar at the top of the window - you will see the SQL needed to create the table on the right
Copy the SQL from the window on the right, and click the Query Tool button in the toolbar, then paste the SQL into the Query window
Delete the any TABLESPACE, ALTER TABLE, GRANT ALL statements at the bottom
Edit the references to the crime schema in the SQL to reference your new schema name, then click the Execute/Refresh (Play) button in the toolbar to create the new table
Click on the new table in the new schema, and click the View Data button in the toolbar. The tool-tip for this button is ‘All Rows’.
In the Data Output window at the bottom, click the Add Row button, then double-click on a cell to add data - try both valid and invalid data to see the result
When you are done, click the Save Data Changes button to commit your changes
You now have a new table with a row of data
In the next exercise, we’ll look in more detail at a table, the objects within it, and how to manage them in pgAdmin.
Add a column
Navigate to crime > street_crime and open the Columns node
You will see that there is a wkb_geometry column - click on this and look at the Properties tab for it on the right - the data type is geometry, meaning that it is a spatial table and so can be used in GIS applications and analysed spatially
Right-click on street_crime > View/Edit Data > All Rows to examine the data
In QGIS, navigate to the same table under your PostgreSQL node in the Browser, and double-click to load it into QGIS
Right-click > Open Attribute Table on the new layer in the Layers panel - you will see the same data as you saw in pgAdmin
In pgAdmin, click on Columns > right-click > Create > Column
Create a new column called officer - this will hold the name of the officer who dealt with the crime
In the Definition tab, set:
Data type: character varying
Length/Precision: 200
Click Save to add the column
Open QGIS, right-click on the layer in the Layers panel and Remove Layer. Add the layer back in to see the new column.
Other objects can be created in a similar way - note also that most objects can either be created from the right-click menu in pgAdmin, or by running SQL in the Query tool.
Review Primary Key and Index
Under street_crime, open the Constraints node, and click on street_crime_pk
Click on the Properties tab on the right
Note that the key icon in the object tree indicates that this is a primary key, and the Columns field on the Properties tab indicates that ogc_fid is the key
Compare the Properties with the SQL (in the SQL tab) used to create the primary key
Under Indexes, look at the street_crime_sidx properties in the same way
You see that the index properties include Access Method (gist, a spatial index) and the Column indexed (wkb_geometry)
If you want to edit these values, click the Edit the Object button in the toolbar on the Properties tab to launch the edit dialog
Access Control
Login and Group Roles control access to database objects. This allows for the creation of Groups with certain privileges, and members of groups (i.e. individual users) who inherit those privileges.
The screenshot below shows that the postgis user is a member of the 'postgis_group' group, and is able to create databases and inherit from its parent.
The next exercise grants access a table to another group.
Access control
Right-click on the new schema and go to Properties
In the Security tab, click on the + sign to the right of Privileges to add a Grantee
Select qgis_group as the Grantee with Usage privileges. Hit save.
From the Servers node, register a new Server, using the same settings as for the database you created at the beginning of the course but with user/password qgis/qgis. It is recommended to call this ‘<server>-qgis>’ as an indication of the role used.
In the new qgis connection in pgAdmin, navigate to the table in the new schema e.g. new_schema.neighbourhood_crime and right-click View/Edit Data > All Rows. The qgis user current has permission to see the table exists due to the schema usage permission but doesn’t have access to the table. Let’s fix that.
Back in the postgis connection, right-click on the table and select Properties
In the Security tab, click on the + sign to the right of Privileges to add a Grantee
Under Grantee, select qgis_group, then under Privileges, check SELECT to grant select privileges to the group, then click Save
In the new qgis connection in pgAdmin, navigate to the table in the new schema e.g. new_schema.neighbourhood_crime and right-click View/Edit Data > All Rows. The qgis user should now be able to see the contents of the table.
The SELECT WITH GRANT OPTION would allow the qgis user to grant select to another role.
All privileges worth knowing at this stage are:
INSERT - the permission to create a new record in the table
UPDATE - the permission to update existing records in the table
DELETE - the permission to remove records from the table
TRUNCATE - the permission to empty the table entirely