Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Adds storage server to self hosting setup

Inian Parameshwaran7d4b72f63778
diff --git a/docker/README.md b/docker/README.md
index 4b0f64e1546..041594405a7 100644
--- a/docker/README.md
+++ b/docker/README.md
@@ -3,7 +3,10 @@
You can run Supabase on your local machine or a server using `docker-compose`.
For a more secure setup to use on a server (e.g. using HTTPS), have a look at the *supabase-traefik* setup.
-## Configuration
+Update the docker-compose file with your valid AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID in the storage section. The key must have permissions to `s3:PutObject , s3:GetObject, s3:DeleteObject` in the bucket you have chosen.
+In production, PGRST_JWT_SECRET and derived keys from the JWT secret like ANON_KEY need to be changed to different values.
+
+You can run Supabase on your local machine using `docker-compose`:
Add your passwords to the .env file.
For better customization and security, please make sure you read the [self-hosting guide](https://supabase.io/docs/guides/self-hosting#running-supabase).
@@ -27,7 +30,9 @@ const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)
Accessing the services directly:
- Kong: http://localhost:8000
- - GoTrue: http://localhost:8000/auth/v1/?apikey=<anon-apikey-from-kong.yml>
- - PostgREST: http://localhost:8000/rest/v1/?apikey=<anon-apikey-from-kong.yml>
- - Realtime: http://localhost:8000/realtime/v1/?apikey=<anon-apikey-from-kong.yml>
-- Postgres: http://localhost:5432
\ No newline at end of file
+ - GoTrue: http://localhost:8000/auth/v1/
+ - PostgREST: http://localhost:8000/rest/v1/
+ - Realtime: http://localhost:8000/realtime/v1/
+ - Supabase: http://localhost:8000/storage/v1
+- Postgres: http://localhost:5432
+
diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml
index 194050ee191..513e427a129 100644
--- a/docker/docker-compose.yml
+++ b/docker/docker-compose.yml
@@ -17,67 +17,57 @@ services:
- ${AUTH_PORT}
depends_on:
- db
- environment:
- GOTRUE_JWT_SECRET: ${JWT_SECRET}
- GOTRUE_JWT_EXP: 3600
- GOTRUE_JWT_DEFAULT_GROUP_NAME: authenticated
- GOTRUE_DB_DRIVER: postgres
- DB_NAMESPACE: auth
- API_EXTERNAL_URL: localhost
- GOTRUE_API_HOST: 0.0.0.0
- PORT: ${AUTH_PORT}
-
- GOTRUE_SMTP_HOST: ${SMTP_HOST}
- GOTRUE_SMTP_PORT: ${SMTP_PORT}
- GOTRUE_SMTP_USER: ${SMTP_USER}
- GOTRUE_SMTP_PASS: ${SMTP_PASS}
-
- GOTRUE_DISABLE_SIGNUP: 'false'
- GOTRUE_SITE_URL: localhost
- GOTRUE_MAILER_AUTOCONFIRM: 'true'
- GOTRUE_LOG_LEVEL: DEBUG
- GOTRUE_OPERATOR_TOKEN: ${OPERATOR_TOKEN}
- DATABASE_URL: 'postgres://postgres:${POSTGRES_PASSWORD}@db:${POSTGRES_PORT}/postgres?sslmode=disable'
-
- rest:
- container_name: supabase-rest
- image: postgrest/postgrest:latest
- ports:
- - ${REST_PORT}:3000
- depends_on:
+ restart: always
+ environment:
+ PGRST_DB_URI: postgres://postgres:postgres@db:5432/postgres
+ PGRST_DB_SCHEMA: public, storage
+ PGRST_DB_ANON_ROLE: postgres
+ PGRST_JWT_SECRET: super-secret-jwt-token-with-at-least-32-characters-long
+ realtime:
+ image: supabase/realtime:latest
+ ports:
+ - '4000:4000'
+ depends_on:
- db
- restart: always
- environment:
- PGRST_DB_URI: postgres://postgres:${POSTGRES_PASSWORD}@db:${POSTGRES_PORT}/postgres
- PGRST_DB_SCHEMA: public
- PGRST_DB_ANON_ROLE: anon
- PGRST_JWT_SECRET: ${JWT_SECRET}
- realtime:
- container_name: supabase-realtime
- image: supabase/realtime:latest
- ports:
- - ${REALTIME_PORT}
- depends_on:
+ restart: on-failure
+ environment:
+ DB_HOST: db
+ DB_NAME: postgres
+ DB_USER: postgres
+ DB_PASSWORD: postgres
+ DB_PORT: 5432
+ PORT: 4000
+ HOSTNAME: localhost
+ # Disable JWT Auth locally. The JWT_SECRET will be ignored.
+ SECURE_CHANNELS: 'false'
+ JWT_SECRET: super-secret-jwt-token-with-at-least-32-characters-long
+ storage:
+ image: supabase/storage-api:v0.1.4
+ ports:
+ - '5000:5000'
+ depends_on:
- db
- restart: on-failure
- environment:
- DB_HOST: db
- DB_NAME: postgres
- DB_USER: postgres
- DB_PASSWORD: ${POSTGRES_PASSWORD}
- DB_PORT: ${POSTGRES_PORT}
- PORT: ${REALTIME_PORT}
- HOSTNAME: localhost
- # Disable JWT Auth locally. The JWT_SECRET will be ignored.
- SECURE_CHANNELS: 'false'
- JWT_SECRET: ${JWT_SECRET}
- db:
- container_name: supabase-db
- build:
- context: ./dockerfiles/postgres
- ports:
- - ${POSTGRES_PORT}:${POSTGRES_PORT}
- command:
+ - rest
+ restart: on-failure
+ environment:
+ ANON_KEY: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYW5vbiIsImlhdCI6MTYxMzUzMTk4NSwiZXhwIjoxOTI5MTA3OTg1fQ.ReNhHIoXIOa-8tL1DO3e26mJmOTnYuvdgobwIYGzrLQ
+ SERVICE_KEY: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoic2VydmljZV9yb2xlIiwiaWF0IjoxNjEzNTMxOTg1LCJleHAiOjE5MjkxMDc5ODV9.FhK1kZdHmWdCIEZELt0QDCw6FIlCS8rVmp4RzaeI2LM
+ PROJECT_REF: bjwdssmqcnupljrqypxz # can be any random string
+ REGION: us-east-1 # region where your bucket is located
+ POSTGREST_URL: http://rest:3000
+ GLOBAL_S3_BUCKET: supa-storage-testing # name of s3 bucket where you want to store objects
+ PGRST_JWT_SECRET: super-secret-jwt-token-with-at-least-32-characters-long
+ DATABASE_URL: postgres://postgres:postgres@db:5432/postgres
+ PGOPTIONS: "-c search_path=storage"
+ AWS_ACCESS_KEY_ID: replace-with-your-aws-key
+ AWS_SECRET_ACCESS_KEY: replace-with-your-aws-secret
+ FILE_SIZE_LIMIT: 52428800
+ db:
+ build:
+ context: ./postgres
+ ports:
+ - 5432:5432
+ command:
- postgres
- -c
- wal_level=logical
diff --git a/docker/dockerfiles/kong/kong.yml b/docker/dockerfiles/kong/kong.yml
index 1adfb5c8c58..659d804783e 100644
--- a/docker/dockerfiles/kong/kong.yml
+++ b/docker/dockerfiles/kong/kong.yml
@@ -39,6 +39,16 @@ services:
- name: key-auth
config:
hide_credentials: true
+- name: storage-v1
+ _comment: 'Storage: /storage/v1/* -> http://storage-api:5000/*'
+ url: http://storage:5000/
+ routes:
+ - name: storage-v1-all
+ strip_path: true
+ paths:
+ - /storage/v1/
+ plugins:
+ - name: cors
consumers:
- username: 'anon-key'
keyauth_credentials:
diff --git a/docker/dockerfiles/postgres/Dockerfile b/docker/dockerfiles/postgres/Dockerfile
index cbec306808c..cd03b29b241 100644
--- a/docker/dockerfiles/postgres/Dockerfile
+++ b/docker/dockerfiles/postgres/Dockerfile
@@ -2,6 +2,7 @@ FROM supabase/postgres:0.13.0
COPY 00-initial-schema.sql /docker-entrypoint-initdb.d/00-initial-schema.sql
COPY auth-schema.sql /docker-entrypoint-initdb.d/auth-schema.sql
+COPY storage-schema.sql /docker-entrypoint-initdb.d/storage-schema.sql
# Build time defaults
ARG build_POSTGRES_DB=postgres
diff --git a/docker/postgres/storage-schema.sql b/docker/postgres/storage-schema.sql
new file mode 100644
index 00000000000..c879c6b6234
--- /dev/null
+++ b/docker/postgres/storage-schema.sql
@@ -0,0 +1,111 @@
+CREATE SCHEMA IF NOT EXISTS storage AUTHORIZATION postgres;
+
+grant usage on schema storage to postgres, anon, authenticated, service_role;
+alter default privileges in schema storage grant all on tables to postgres, anon, authenticated, service_role;
+alter default privileges in schema storage grant all on functions to postgres, anon, authenticated, service_role;
+alter default privileges in schema storage grant all on sequences to postgres, anon, authenticated, service_role;
+
+DROP TABLE IF EXISTS "storage"."buckets";
+CREATE TABLE "storage"."buckets" (
+ "id" text not NULL,
+ "name" text NOT NULL,
+ "owner" uuid,
+ "created_at" timestamptz DEFAULT now(),
+ "updated_at" timestamptz DEFAULT now(),
+ CONSTRAINT "buckets_owner_fkey" FOREIGN KEY ("owner") REFERENCES "auth"."users"("id"),
+ PRIMARY KEY ("id")
+);
+CREATE UNIQUE INDEX "bname" ON "storage"."buckets" USING BTREE ("name");
+
+DROP TABLE IF EXISTS "storage"."objects";
+CREATE TABLE "storage"."objects" (
+ "id" uuid NOT NULL DEFAULT extensions.uuid_generate_v4(),
+ "bucket_id" text,
+ "name" text,
+ "owner" uuid,
+ "created_at" timestamptz DEFAULT now(),
+ "updated_at" timestamptz DEFAULT now(),
+ "last_accessed_at" timestamptz DEFAULT now(),
+ "metadata" jsonb,
+ CONSTRAINT "objects_bucketId_fkey" FOREIGN KEY ("bucket_id") REFERENCES "storage"."buckets"("id"),
+ CONSTRAINT "objects_owner_fkey" FOREIGN KEY ("owner") REFERENCES "auth"."users"("id"),
+ PRIMARY KEY ("id")
+);
+CREATE UNIQUE INDEX "bucketid_objname" ON "storage"."objects" USING BTREE ("bucket_id","name");
+CREATE INDEX name_prefix_search ON storage.objects(name text_pattern_ops);
+
+ALTER TABLE storage.objects ENABLE ROW LEVEL SECURITY;
+
+CREATE OR REPLACE FUNCTION storage.foldername(name text)
+ RETURNS text[]
+ LANGUAGE plpgsql
+AS $function$
+DECLARE
+_parts text[];
+BEGIN
+ select string_to_array(name, '/') into _parts;
+ return _parts[1:array_length(_parts,1)-1];
+END
+$function$;
+
+CREATE OR REPLACE FUNCTION storage.filename(name text)
+ RETURNS text
+ LANGUAGE plpgsql
+AS $function$
+DECLARE
+_parts text[];
+BEGIN
+ select string_to_array(name, '/') into _parts;
+ return _parts[array_length(_parts,1)];
+END
+$function$;
+
+CREATE OR REPLACE FUNCTION storage.extension(name text)
+ RETURNS text
+ LANGUAGE plpgsql
+AS $function$
+DECLARE
+_parts text[];
+_filename text;
+BEGIN
+ select string_to_array(name, '/') into _parts;
+ select _parts[array_length(_parts,1)] into _filename;
+ return split_part(_filename, '.', 2);
+END
+$function$;
+
+CREATE OR REPLACE FUNCTION storage.search(prefix text, bucketname text, limits int DEFAULT 100, levels int DEFAULT 1, offsets int DEFAULT 0)
+ RETURNS TABLE (
+ name text,
+ id uuid,
+ updated_at TIMESTAMPTZ,
+ created_at TIMESTAMPTZ,
+ last_accessed_at TIMESTAMPTZ,
+ metadata jsonb
+ )
+ LANGUAGE plpgsql
+AS $function$
+DECLARE
+_bucketId text;
+BEGIN
+ select buckets."id" from buckets where buckets.name=bucketname limit 1 into _bucketId;
+ return query
+ with files_folders as (
+ select ((string_to_array(objects.name, '/'))[levels]) as folder
+ from objects
+ where objects.name ilike prefix || '%'
+ and bucket_id = _bucketId
+ GROUP by folder
+ limit limits
+ offset offsets
+ )
+ select files_folders.folder as name, objects.id, objects.updated_at, objects.created_at, objects.last_accessed_at, objects.metadata from files_folders
+ left join objects
+ on prefix || files_folders.folder = objects.name
+ where objects.id is null or objects.bucket_id=_bucketId;
+END
+$function$;
+
+GRANT ALL PRIVILEGES ON SCHEMA storage TO postgres;
+GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA storage TO postgres;
+GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA storage TO postgres;
\ No newline at end of file