forked from s_ranjbar/city_retrofit
67 lines
4.2 KiB
Markdown
67 lines
4.2 KiB
Markdown
## Installing PostgreSQL Database Server on Linux (Ubuntu) ##
|
|
Execute the *install_postgresql_linux.sh* script to install PostgreSQL database
|
|
*NB: PostgreSQL DB Server runs on a default port of 5432.*
|
|
|
|
## Installing PostgreSQL Database Server on Windows ##
|
|
1. Download a Windows installer from [this link](https://www.enterprisedb.com/downloads/postgres-postgresql-downloads).
|
|
2. Double click on the installer file and follow the prompts of the installation wizard
|
|
3. On the component selection page of the installation wizard make sure to select *PostgreSQL Server and Commandline tools*
|
|
4. You can optionally select pgAdmin 4 to install a graphical UI to access your database
|
|
5. On the password page when prompted, enter the default password (postgres) and confirm it
|
|
6. 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.
|
|
7. 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 ##
|
|
1. Download the Mac OS X installer from [this link](https://www.enterprisedb.com/downloads/postgres-postgresql-downloads).
|
|
2. 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 ##
|
|
1. 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
|
|
2. Execute `create user <username> with encrypted password '<password>';` in the psql console to create a user.
|
|
3. Execute `create database <database-name>;` in the psql console to create a database.
|
|
4. 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.
|
|
5. 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](https://www.postgresqltutorial.com/postgresql-administration/postgresql-create-schema/).
|
|
|
|
**NB: You can grant selected privileges to the user on the database using commands [on this page](https://tableplus.com/blog/2018/04/postgresql-how-to-grant-access-to-users.html).*
|
|
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
|
|
1. Create a .env file that contains the configuration parameters as explained in the *Database Configuration Parameters*
|
|
section in persistence/README.md file.
|
|
2. The .env file should contain the following credentials: database user, database password, database host an,d database port
|
|
3. Provide the *absolute path* to the .env file to the persistence importers and exporters whenever using them in your code
|
|
as shown below:
|
|
```python
|
|
from 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
|
|
```python
|
|
from 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)
|
|
```
|
|
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**
|