Quickstart step 2: Initialize your database
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 - _postgres
createdb
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.Add these, for example, in your
~/.bashrc
:export PGPORT=5432 export PGHOST=localhost export PGUSER=sourcegraph export PGPASSWORD=sourcegraph export PGDATABASE=sourcegraph export PGSSLMODE=disable
You can also use a tool like
envdir
or a.dotenv
file to source these env vars on demand when you start the server.
With Docker
You may also want to run Postgres within a docker container instead of as a system service. Running within a container provides some advantages such as storing the data seperately from the container, you do not need to run it as a system service and its easy to use different database versions or multiple databases.
Create a directory to store and mount the database from for persistence:
# Create a seperate dir to store the database mkdir PGDATA_DIR # Also add this to your '~/.bashrc' export PGDATA_DIR=/path/to/PGDATA_DIR/
Run the container:
docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=sourcegraph \ -e POSTGRES_USER=sourcegraph -e POSTGRES_INITDB_ARGS=" --encoding=UTF8 " \ -v $PGDATA_DIR:/var/lib/postgresql/data postgres
Ensure you can connect to the database in your container using
docker exec -it <container-id> psql -U sourcegraph
and enter passwordsourcegraph
. You can use the commanddocker ps
to view the container-id.Configure database settings in your environment:
The Sourcegraph server reads PostgreSQL connection configuration from the
PG*
environment variables.Add these, for example, in your
~/.bashrc
:export PGPORT=5432 export PGHOST=localhost export PGUSER=sourcegraph export PGPASSWORD=sourcegraph export PGDATABASE=sourcegraph export PGSSLMODE=disable
You can also use a tool like
envdir
or a.dotenv
file to source these env vars on demand when you start the server.On restarting docker, you may need to start the container again. Find the image with
docker ps --all
and thendocker run <$containerID>
to start again.
More info
For more information about data storage, read our full PostgreSQL page.
Migrations are applied automatically.