Integrating Database to Swordbattle V1 code V1 Database Integration Guide


This documentation provides instructions for setting up a PostgreSQL database to work with the code. It outlines the steps to initialize the database and configure the environment so you have functionality like shop, accounts, leaderboards, etc locally.


  1. A running PostgreSQL instance. (You can use Render as a free option)
  2. Administrative rights to create tables in the PostgreSQL instance.
  3. Running application codebase.

Steps to Connect to the Database

1. Initialize the Database

Before you connect the application to the database, you need to initialize the required tables. Execute the following SQL queries to set up the necessary tables:

-- Table for account details
CREATE TABLE public.accounts (
  skins jsonb NULL,
  password text NOT NULL,
  secret text NOT NULL,
  email text NULL,
  username text NOT NULL,
  created_at timestamp without time zone NOT NULL DEFAULT now(),
  id serial NOT NULL,
  lastusernamechange timestamp(6) without time zone NULL
ALTER TABLE public.accounts ADD CONSTRAINT accounts_pkey PRIMARY KEY (id);

-- Table for game details
  "time" bigint NULL,
  killerverified boolean NULL,
  verified boolean NULL,
  name text NULL,
  killedby text NULL,
  created_at timestamp without time zone NULL DEFAULT now(),
  kills bigint NULL,
  coins bigint NULL,
  id serial NOT NULL

-- Table for game stats
CREATE TABLE public.stats (
  id serial NOT NULL,
  username text NOT NULL,
  game_time bigint NOT NULL DEFAULT 0,
  game_count integer NOT NULL DEFAULT 0,
  stabs integer NOT NULL DEFAULT 0,
  coins integer NOT NULL DEFAULT 0
ALTER TABLE public.stats ADD CONSTRAINT stats_pkey PRIMARY KEY (id);
ALTER TABLE stats ADD CONSTRAINT unique_username UNIQUE (username, game_date);

-- Table for content details
CREATE TABLE public.content (
  id serial NOT NULL,
  created_at timestamp without time zone NOT NULL DEFAULT now(),
  link text NULL,
  title text NULL,
  author text NULL,
  chance integer NOT NULL DEFAULT 1,
  source text NOT NULL DEFAULT 'youtube'::text,
  label text NULL
ALTER TABLE public.content ADD CONSTRAINT content_pkey PRIMARY KEY (id);

2. Set Environment Variable

To enable the application to connect to the PostgreSQL database, set the DATABASE_URL environment variable to your Postgres URI (in .env file):


Replace the placeholders with your actual PostgreSQL connection details.

Note: It’s essential to keep this URI confidential, as it contains sensitive information.


After following the steps outlined above, the application should be integrated with your PostgreSQL database. Remember to secure your database and application, ensure regular backups, and monitor for any anomalies or issues.

Feel free to make a new topic with the dev tag to ask any questions or report any bugs regarding this process.


Whatwill this do for us? Will we just be able to manippulate our own version of sb with real life data?

1 Like

what does this mean, and what impact will it have ._.

1 Like

Commit 0ed38f6 introduces Database Free Mode, which allows dev to run swordbattle independent of a database. It does not affect the main code. The main purpose of this is to simplify the swordbattle local development process.

This guide can be used by devs who want to include a database to enable functionality like leaderboards, accounts, shop, etc. It does not affect the main game in any way.

1 Like

which means, devs can get a copy of the code and run it on their own

It was already possible, code can be found on Github!


Oh, that’s cool I can make a version of sb with hacks

You can try, but it would be your own Sb, not the main game so no one would care.

1 Like

People definitely would depending on what is done to it.


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.