-
Notifications
You must be signed in to change notification settings - Fork 0
Configure PostgreSQL database
At the end of this guide you will:
- set up PostgreSQL as a default project database
git checkout bp-models
You will need docker-engine
and docker-compose
installed on your computer to follow this
guide.
In case, you have no these installed refer to Get project database running, and after that continue from Set up environment variables.
The initialization query for the docker postgres container can be found at docker/db/init.sql. And it looks like:
CREATE ROLE tasktracker WITH ENCRYPTED PASSWORD 'tasktracker' LOGIN CREATEDB;
COMMENT ON ROLE tasktracker IS 'tasktracker database maintenance role';
CREATE DATABASE tasktracker OWNER tasktracker;
COMMENT ON DATABASE tasktracker IS 'tasktracker project database';
This will create a maintenance role and a project database when the db
container starts for the first
time.
You can check the connection to the database using psql or pgAdmin clients. pgAdmin is also available as a docker container for this project. Refer to README file for more info.
This is an optional step, required only if you want to use pgAdmin container within this project.
pgAdmin is one of the most popular PostgreSQL clients. Starting with version 4.x, it uses a web-based UI running in
your web browser. The pgAdmin container exposes its 80 port to the host machine. By default, this port is mapped to
5050. If port 5050 is already occupied by other software on your system, you may set up any available port by using
the PGADMIN_PORT
environment variable.
After running pgAdmin, visit http://localhost:5050 in your web browser (adjust the port number if needed).
Password | |
---|---|
[email protected] | pgadmin |
When connecting to the PostgreSQL server via pgAdmin, use "postgresql-server" as the alias for the db container. This connection is already defined in the servers.json file under the docker/pgadmin directory, so there is no need to connect manually.
Note that it may take some time for the container to set up and run the internal server.
To start container use command:
docker compose up -d pgadmin
This will also start the db
container.
The db service runs the PostgreSQL server container. It exposes port 5432 to the host machine, allowing you to use it
as if you had PostgreSQL running locally. The default port mapping is "5432:5432". If you already have port 5432
occupied by other software, you may set up any available port by using the POSTGRES_PORT
environment
variable.
username | password |
---|---|
postgres | postgres |
tasktracker | tasktracker |
You can run this service separately from other services defined in the Compose file with:
docker compose up -d db
In your .env file complete the environment variables set up.
POSTGRES_DB=tasktracker POSTGRES_USER=tasktracker POSTGRES_PASSWORD=tasktracker POSTGRES_HOST=db POSTGRES_PORT=5432
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"HOST": os.environ.get("PGHOST", "localhost"),
"PORT": os.environ.get("PGPORT", "5432"),
"NAME": os.environ.get("PGDATABASE"),
"USER": os.environ.get("PGUSER"),
"PASSWORD": os.environ.get("PGPASSWORD"),
}
}
python manage.py migrate
- Set up new Django project
- Add project views
- Configure PostgreSQL database
- Add task model
- Fetch data from the database to response
- Render HTML templates to response
- Create auth forms
- Implement user authentication
- Create custom user model
- Create task model form
- Apply permissions to views
- Refactoring project views to CBVs
- Create serializers
- Implement API views
- Apply API views permissions