Skip to content

Commit

Permalink
add postgis to docker-compose. fixed error for geohub-database.sql, a… (
Browse files Browse the repository at this point in the history
#2624)

* add postgis to docker-compose. fixed error for geohub-database.sql, and created a script init.sh to initialise database schema for geohub schema

* update Makefile
  • Loading branch information
JinIgarashi authored Jan 3, 2024
1 parent 06c66b7 commit b3c05fe
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 39 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ help:
@echo " dev to run dev server for GeoHub"
@echo " docker-build to build Docker image for dev mode"
@echo " docker-up to run Docker image in dev mode"
@echo " docker-down to destroy Docker container in dev mode"
@echo " docker-prod to build and run production Docker image"
@echo " docker-prod-build to build production Docker image"
@echo " docker-prod-run to run production Docker image"
Expand Down Expand Up @@ -48,6 +49,13 @@ docker-up:
@echo "------------------------------------------------------------------"
docker-compose -f docker/docker-compose.yml up

docker-down:
@echo
@echo "------------------------------------------------------------------"
@echo "Destroy docker containers"
@echo "------------------------------------------------------------------"
docker-compose -f docker/docker-compose.yml down

shell:
@echo
@echo "------------------------------------------------------------------"
Expand Down
71 changes: 35 additions & 36 deletions backends/database/geohub-database.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE SCHEMA IF NOT EXISTS geohub;

CREATE TABLE geohub.dataset
CREATE TABLE IF NOT EXISTS geohub.dataset
(
id character varying NOT NULL,
url character varying NOT NULL,
Expand Down Expand Up @@ -34,20 +35,7 @@ CREATE INDEX IF NOT EXISTS dataset_bounds_geom_idx
ON geohub.dataset USING gist
(bounds)

CREATE TABLE geohub.dataset_tag
(
dataset_id character varying NOT NULL,
tag_id serial NOT NULL,
CONSTRAINT dataset_tag_pkey PRIMARY KEY (dataset_id, tag_id)
);

COMMENT ON TABLE geohub.dataset_tag IS 'this table connects file_metadata and tag tables';

COMMENT ON COLUMN geohub.dataset_tag.dataset_id IS 'unique ID for dataset';

COMMENT ON COLUMN geohub.dataset_tag.tag_id IS 'unique ID for tag name';

CREATE TABLE geohub.dataset_defaultstyle
CREATE TABLE IF NOT EXISTS geohub.dataset_defaultstyle
(
dataset_id character varying NOT NULL,
layer_id character varying NOT NULL,
Expand Down Expand Up @@ -80,7 +68,7 @@ COMMENT ON COLUMN geohub.dataset_defaultstyle.classification_method IS 'classifi
COMMENT ON COLUMN geohub.dataset_defaultstyle.classification_method_2 IS 'classification method if there are two classification settings (icon size, line width) apart from color.';


CREATE TABLE geohub.style
CREATE TABLE IF NOT EXISTS geohub.style
(
id serial NOT NULL,
name character varying NOT NULL,
Expand All @@ -96,7 +84,7 @@ CREATE TABLE geohub.style

COMMENT ON TABLE geohub.style IS 'this table manages style.json created at geohub';

CREATE TABLE geohub.style_favourite
CREATE TABLE IF NOT EXISTS geohub.style_favourite
(
style_id integer NOT NULL,
user_email character varying(100) NOT NULL,
Expand All @@ -118,7 +106,7 @@ COMMENT ON COLUMN geohub.style_favourite.user_email IS 'user email address';
COMMENT ON COLUMN geohub.style_favourite.savedat IS 'saved datetime';


CREATE TABLE geohub.tag
CREATE TABLE IF NOT EXISTS geohub.tag
(
id serial NOT NULL,
value character varying NOT NULL,
Expand All @@ -134,6 +122,19 @@ COMMENT ON COLUMN geohub.tag.value IS 'tag value';

COMMENT ON COLUMN geohub.tag.key IS 'tag key';

CREATE TABLE IF NOT EXISTS geohub.dataset_tag
(
dataset_id character varying NOT NULL,
tag_id serial NOT NULL,
CONSTRAINT dataset_tag_pkey PRIMARY KEY (dataset_id, tag_id)
);

COMMENT ON TABLE geohub.dataset_tag IS 'this table connects file_metadata and tag tables';

COMMENT ON COLUMN geohub.dataset_tag.dataset_id IS 'unique ID for dataset';

COMMENT ON COLUMN geohub.dataset_tag.tag_id IS 'unique ID for tag name';

ALTER TABLE geohub.dataset_tag
ADD CONSTRAINT FK_tag_TO_dataset_tag
FOREIGN KEY (tag_id)
Expand All @@ -159,7 +160,7 @@ CREATE INDEX IF NOT EXISTS tag_idx_value
(value COLLATE pg_catalog."default" ASC NULLS LAST)
TABLESPACE pg_default;

CREATE TABLE geohub.dataset_favourite
CREATE TABLE IF NOT EXISTS geohub.dataset_favourite
(
dataset_id character varying NOT NULL,
user_email character varying(100) NOT NULL,
Expand All @@ -184,8 +185,6 @@ COMMENT ON COLUMN geohub.dataset_favourite.user_email
COMMENT ON COLUMN geohub.dataset_favourite.savedat
IS 'timestamp which users saved';

DROP TABLE IF EXISTS geohub.country;

CREATE TABLE IF NOT EXISTS geohub.country
(
iso_3 character varying NOT NULL,
Expand All @@ -199,24 +198,24 @@ CREATE TABLE IF NOT EXISTS geohub.country
region3_code integer NOT NULL,
region3_name character varying NOT NULL,
CONSTRAINT country_pkey PRIMARY KEY (iso_3)
)
);

-- superuser table
CREATE TABLE geohub.superuser
CREATE TABLE IF NOT EXISTS geohub.superuser
(
user_email character varying(100) NOT NULL,
createdat timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT superuser_pkey PRIMARY KEY (user_email)
);

ALTER TABLE IF EXISTS geohub.superuser
OWNER to undpgeohub;
-- ALTER TABLE IF EXISTS geohub.superuser
-- OWNER to undpgeohub;

COMMENT ON TABLE geohub.superuser
IS 'this table manages superusers across geohub app';

-- dataset_permission table
CREATE TABLE geohub.dataset_permission
CREATE TABLE IF NOT EXISTS geohub.dataset_permission
(
dataset_id character varying NOT NULL,
user_email character varying(100) NOT NULL,
Expand All @@ -231,8 +230,8 @@ CREATE TABLE geohub.dataset_permission
NOT VALID
);

ALTER TABLE IF EXISTS geohub.dataset_permission
OWNER to undpgeohub;
-- ALTER TABLE IF EXISTS geohub.dataset_permission
-- OWNER to undpgeohub;

COMMENT ON TABLE geohub.dataset_permission
IS 'this table manages users'' permission for operating each dataset';
Expand All @@ -241,15 +240,15 @@ COMMENT ON COLUMN geohub.dataset_permission.permission
IS '1: read, 2: read/write, 3: owner';


CREATE TABLE geohub.user_settings
CREATE TABLE IF NOT EXISTS geohub.user_settings
(
user_email character varying(100) NOT NULL,
settings json NOT NULL,
CONSTRAINT user_settings_pkey PRIMARY KEY (user_email)
);

ALTER TABLE IF EXISTS geohub.user_settings
OWNER to undpgeohub;
-- ALTER TABLE IF EXISTS geohub.user_settings
-- OWNER to undpgeohub;

COMMENT ON TABLE geohub.user_settings
IS 'This table stores user settings';
Expand All @@ -260,7 +259,7 @@ COMMENT ON COLUMN geohub.user_settings.user_email
COMMENT ON COLUMN geohub.user_settings.settings
IS 'This column stores user settings in json format';

CREATE TABLE geohub.users
CREATE TABLE IF NOT EXISTS geohub.users
(
id character varying NOT NULL,
user_email character varying(100) NOT NULL,
Expand All @@ -269,8 +268,8 @@ CREATE TABLE geohub.users
CONSTRAINT users_pkey PRIMARY KEY (id)
);

ALTER TABLE IF EXISTS geohub.users
OWNER to undpgeohub;
-- ALTER TABLE IF EXISTS geohub.users
-- OWNER to undpgeohub;

COMMENT ON TABLE geohub.users
IS 'This table manages the login users information for analysis';
Expand All @@ -287,14 +286,14 @@ COMMENT ON COLUMN geohub.users.signupat
COMMENT ON COLUMN geohub.users.lastaccessedat
IS 'date time when user accessed last time';

CREATE TABLE geohub.stac
CREATE TABLE IF NOT EXISTS geohub.stac
(
id character varying NOT NULL,
name character varying NOT NULL,
url character varying NOT NULL,
type character varying NOT NULL,
providers json,
createdat timestamp with time zone NOT NULL DEFAULT now(),,
createdat timestamp with time zone NOT NULL DEFAULT now(),
created_user character varying(100) NOT NULL,
updatedat timestamp with time zone,
updated_user character varying(100) ,
Expand Down
11 changes: 11 additions & 0 deletions backends/database/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

USER=docker
DATABASE=geodata
HOST=localhost
POST=25432

echo
echo "------------------------------------------------------------------"
echo "Initialising geodata database"
echo "------------------------------------------------------------------"
psql -f ./geohub-database.sql -U $USER -d $DATABASE -h $HOST -p $POST
12 changes: 11 additions & 1 deletion docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cp .env.docker.example .env
cd ../..
```

also, copy .env to `docker` folder for the database connection string for pg_tileserv
also, copy .env to `docker` folder for the database connection string for pg_tileserv if you are going to connect to remote database server.

```shell
cp sites/geohub/.env docker/.env
Expand All @@ -33,6 +33,16 @@ The following backend services will be launched in docker.
- titiler: http://localhost:8000
- pgtileserv: http://localhost:7800
- static API: http://localhost:9000
- PostGIS:
- port=25432
- user=docker
- pass=docker

for the first time, you need to setup database table by the following command in another terminal. Password is `docker` if you have not changed from default.

```shell
./backends/database/init.sh
```

Then, open another terminal to launch sveltekit by the following command at root folder of the repository.

Expand Down
19 changes: 18 additions & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
version: "3"

volumes:
database:

services:
geohub:
image: undp-data/geohub:latest
Expand Down Expand Up @@ -36,6 +39,20 @@ services:
- ../packages:/app/packages
# entrypoint: pnpm --filter="./sites/geohub" dev --host

db:
image: kartoza/postgis:13.0
container_name: "geohub_db"
volumes:
- database:/var/lib/postgresql/13
environment:
- ALLOW_IP_RANGE=0.0.0.0/0
- POSTGRES_DBNAME=${DATABASE_NAME:-geodata}
- POSTGRES_USER=${DATABASE_USERNAME:-docker}
- POSTGRES_PASS=${DATABASE_PASSWORD:-docker}
restart: on-failure:5
ports:
- 25432:5432

cogserver:
image: ghcr.io/undp-data/cogserver:latest
container_name: cogserver
Expand Down Expand Up @@ -68,7 +85,7 @@ services:
container_name: pg_tileserv
restart: unless-stopped
environment:
- DATABASE_URL=${DATABASE_CONNECTION:-''}
- DATABASE_URL=${DATABASE_CONNECTION:-'postgres://docker:docker@geohub_db:5432/geodata?sslmode=disable'}
ports:
- 7800:7800

Expand Down
2 changes: 1 addition & 1 deletion sites/geohub/.env.docker.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ AZURE_STORAGE_ACCOUNT_UPLOAD=
AZURE_STORAGE_ACCESS_KEY_UPLOAD=
AZURE_SERVICE_BUS_CONNECTIONSTRING=
AZURE_SERVICE_BUS_QUEUE_NAME=
DATABASE_CONNECTION=
DATABASE_CONNECTION=postgres://docker:docker@localhost:5432/geodata?sslmode=disable
# Private dynamic variables for Azure Active Directory (imported via $env/dynamic/private in Auth.js)
# these dynamic variables are loaded from `.env` in local, and they are loaded from application settings in server
# These variables need to be registered in Azure AppService from portal in production
Expand Down

0 comments on commit b3c05fe

Please sign in to comment.