* doc: Clear up quickstart step order swap step 2 (DB initialization) and step 3 (Run docker) in our quickstart make `With docker` the first section in the DB initialization 1. We need to install docker anyway, so we might as well do it before initializing the DB 2. It is easier to init the DB with docker, because it’s just 2 commands vs a lot if we do not run docker. 3. We are not following a natural order of things, if we allow using docker in step 2 and then basically require to install it in step 3 * Fix step index numbers for swapped steps * Fix broken links
2.8 KiB
Quickstart step 3: Initialize your database
With Docker
The Sourcegraph server reads PostgreSQL connection configuration from the PG* environment variables.
The development server startup script as well as the docker compose file provide default settings, so it will work out of the box.
To initialize your database, you may have to set the appropriate environment variables before running the createdb command:
export PGUSER=sourcegraph PGPASSWORD=sourcegraph PGDATABASE=sourcegraph
createdb --user=sourcegraph --owner=sourcegraph --encoding=UTF8 --template=template0 sourcegraph
You can also use the PGDATA_DIR environment variable to specify a local folder (instead of a volume) to store the database files. See the dev/redis-postgres.yml file for more details.
This can also be spun up using sg start redis-postgres, with the following sg.config.override.yaml:
env:
PGHOST: localhost
PGPASSWORD: sourcegraph
PGUSER: sourcegraph
Without Docker
You need a fresh Postgres database and a database user that has full ownership of that database.
-
Create a database for the current Unix user
# For Linux users, first access the postgres user shell sudo su - postgres # For Mac OS users sudo su - _postgrescreatedb -
Create the Sourcegraph user and password
createuser --superuser sourcegraph psql -c "ALTER USER sourcegraph WITH PASSWORD 'sourcegraph';" -
Create the Sourcegraph database
createdb --owner=sourcegraph --encoding=UTF8 --template=template0 sourcegraph -
Configure database settings in your environment
The Sourcegraph server reads PostgreSQL connection configuration from the
PG*environment variables.The startup script sets default values that work with the setup described here, but if you are using different values you can overwrite them, for example, in your
~/.bashrc:export PGPORT=5432 export PGHOST=localhost export PGUSER=sourcegraph export PGPASSWORD=sourcegraph export PGDATABASE=sourcegraph export PGSSLMODE=disableYou can also use a tool like
envdiror a.dotenvfile to source these env vars on demand when you start the server.
More info
For more information about data storage, read our full PostgreSQL page.
Migrations are applied automatically.