4.6 KiB
Installing PostgreSQL Database Server on Linux (Ubuntu)
In the terminal, add the key to the keyring
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
Update your repositories with
sudo apt-get update
Install postgresql
sudo apt-get install postgresql ` NB: PostgreSQL DB Server runs on a default port of 5432.
Installing PostgreSQL Database Server on Windows
- Download a Windows installer from this link.
- Double click on the installer file and follow the prompts of the installation wizard
- On the component selection page of the installation wizard make sure to select PostgreSQL Server and Commandline tools
- You can optionally select pgAdmin 4 to install a graphical UI to access your database
- On the password page when prompted, enter the default password (postgres) and confirm it
- You can change the default password of 5432 on the port page. You should ensure that whatever port number you provide is not used by another service.
- Follow the installation wizard prompt to complete your installation. You can verify your installation by searching for the psql tool from your start menu
Installing PostgreSQL Database Server on Mac OS X
- Download the Mac OS X installer from this link.
- Launch the installation wizard by double-clicking it and follow the rest of steps as described above on Installing PostgreSQL Database Server on Windows.
NB: Hub has been tested with version 15 of PostgreSQL
Create Database and Database User
- Connect to the PostgreSQL database server via psql by executing
sudo -u postgres psql
. You will be be prompted for a password, the default password of postgres user is postgres. The above command may not work on a Windows system using the default command line tool. You can access the psql tool from Windows start menu and follow the rest of the instructions from step 2 below - Execute
create user <username> with encrypted password '<password>';
in the psql console to create a user. - Execute
create database <database-name>;
in the psql console to create a database. - Execute
grant all privileges on database <database-name> to <username>;
to grant the all privileges on the database to the user created in step 2 above. - The created database by default, has on schema named public which you can use. However, if you wish to create another schema, you can do so by following this link.
NB: You can grant selected privileges to the user on the database using commands on this page. The user should however have read and write permission to all tables in the database. You can as well create a database and user using the PgAdmin UI tool
Setting Up Database Connection Parameters
- Create a .env file that contains the configuration parameters as explained in the Database Configuration Parameters section in persistence/README.md file.
- The .env file should contain the following credentials: database user, database password, database host an,d database port
- Provide the absolute path to the .env file to the persistence importers and exporters whenever using them in your code as shown below:
from hub.exports.db_factory import DBFactory
from pathlib import Path
dotenv_path = (Path(__file__).parent / '.env').resolve()
factory = DBFactory(db_name='hub_db', app_env='PROD', dotenv_path=dotenv_path)
Create Database Tables
Use the DBSetup class in the persistence package to create the required database tables as described below
from hub.persistence import DBSetup
from pathlib import Path
dotenv_path = (Path(__file__).parent / '.env').resolve()
DBSetup(db_name='hub_db', app_env='PROD', dotenv_path=dotenv_path, admin_password="your password here", application_uuid="your admin application uuid")
The DBSetUp class also creates a default admin user with default credentials that can be changed. with the import UserFactory class. The admin user (name, email, password and role) is logged into the console after it is created by the constructor of DBSetup. Use can also manage users (create, read, update and delete) with user import and export factories.
NB: Make sure to change the default admin user credentials