Database best practices

Database best practices


This short article is designed to help you better understand how to get the most out of your data in both XMAP and QGIS by applying simple but effective 'best practice' principles to your PostGIS database. 

The golden rules:

  1. Schema names, table names and column names are always kept in LOWER CASE
  2. Schema names, table names and column names never have spaces between words, spaces are replaced with UNDERSCORES
  3. Schema names, table names and column names never contain special characters or start with numbers
  4. All tables must have a PRIMARY KEY, with a preference for an ID column set as a SERIAL type. 

Following the golden rules: 

Schema names: 
planning_policy_local_plan_2019_to_2031 - A good example of following the first 3 golden rules. No upper cases, no spaces and no special characters
Planning Policy Local_Plan_2019 to 2031 - A bad example with a mixture of cases, and containing spaces. 
Table names:
msc_conservation_areas - A good example of following the first 3 golden rules. No upper cases, no spaces and no special characters
2019_Conservation_Areas - A bad example, starts with a number and is mixed case
Column names:
ca_name - A good example of following the first 3 golden rules. No upper cases, no spaces and no special characters
CA_name - A bad example, mixed case

Primary Keys and Serials:
A Primary Key is a defined column into your table that contains a unique value (normally a number), and can not be set (or left) as null. To make database administration easier, setting the Primary Key column to a type of serial is the best option, as will take care of things like sequences for you. While you can have Primary Key column types set to integer, there comes a lot more SQL admin when it comes to applying permissions so that a user can edit the table. 

Create table and grant permissions using the best golden rules:
  1.             CREATE TABLE planning_policy_local_plan_2019_to_2031. msc_conservation_areas (
  2.                   gxid SERIAL PRIMARY KEY,
  3.                   ca_name text,
  4.                   geom GEOMETRY(MULTIPOLYGON, 27700) 
  5.             );
  6.            GRANT ALL PRIVILEGES ON TABLE  planning_policy_local_plan_2019_to_2031.msc_conservation_areas TO fred;
  7.            GRANT ALL PRIVILEGES ON SEQUENCE msc_conservation_areas_gxid_seq TO fred;

Create table and grant permissions not following the golden rules. Note how harder it is to read and how much more SQL you need to write

  1.             CREATE TABLE "Planning Policy Local_Plan_2019 to 2031". "2019_Conservation_Areas" (
  2.                   gxid int4,
  3.                   "CA_name" text,
  4.                   geom GEOMETRY(MULTIPOLYGON, 27700) 
  5.             );

You will have to create a sequence for the primary key as well, which is not needed when using a serial type. 

  1. CREATE SEQUENCE "2019_Conservation_Areas_gxid_seq"
  2.     START 1
  3.     INCREMENT 1
  4.     MINVALUE 1
  5.     NO MAXVALUE
  6.     CACHE 1;

You will have to update the table to let Postgres know which column is to be used as a primary key

  1. ALTER TABLE "Planning Policy Local_Plan_2019 to 2031"."2019_Conservation_Areas" ALTER COLUMN gxid SET DEFAULT nextval('regions_gxid_seq');
  2. GRANT ALL PRIVILEGES ON SEQUENCE "2019_Conservation_Areas_gxid_seq" TO fred;



    • Related Articles

    • Publishing Layers from a Remote Database

      Overview XMAP supports connections to any PostGIS database of version 9.6 or greater. This database can be hosted and managed by us or you. If you have a remote database you can use this to connect to many software applications to improve access to ...
    • Database Manager

      Overview The Database Manager is made available if your organisation has 1 or more dedicated (private) PostgreSQL databases linked to it. It exposes administration functionality for you to manage your database at a high level without the need to ...
    • Connecting to a Cloud Database through Microsoft Access

      Overview This article is for connecting to a cloud database in order to edit non-spatial data through Microsoft Access. This guide is for those using Windows. Method Required Installations Install Microsoft Access (if not installed already) Install ...
    • Behind the Scenes at Geoxphere

      How We Keep Your Services Running Smoothly By Tom Huntley. October 2024 At Geoxphere, we use the power of cloud computing to deliver secure, reliable, and user-friendly geospatial tools to customers across the UK. Much of this work is hidden behind ...
    • Using your XMAP layers in QGIS

      Overview For customers who have a dedicated database connected to XMAP, you can access your data directly in QGIS. This means that both XMAP and QGIS are reading from the same source, so there is no risk of multiple versions of your data existing in ...