Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

port workshop formatting to setup guide #491

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 73 additions & 11 deletions guides/db-setup/readme.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,98 @@
# Local database setup ⚙️

## Initiating local database
## Initialising the Database

Once you have your schema ready, make sure you create a new database for yourself locally. For example, let's call it `videorec`. From a shell use `createdb` to create a new database for yourself:
### Create a local database

Use the `createdb` command to create a new database called `videorec`:

```bash
createdb videorec
```

Once it is created you can now try to load your file into the database using `psql`:
### Create tables and load data

Create tables and load the database with initial data:

```bash
psql -d videorec < db/initdb.sql
```

**Note:** Depending on how postgresql was installed for you, you might need to add some connectivity details to both `createdb` and `psql`:
#### Summary

1. `psql` : Use the PostgreSQL command-line interface.
2. `-d` : This flag marks the next argument as the database name.
3. `videorec` : The name of the database you want to populate.
4. `<` : The following file will be used as input.
5. `db/initdb.sql` : The path to the SQL file to populate the database.

Depending how postgresql was installed for you, you might need to add some connectivity details to both createdb and psql: `psql -h localhost -U username -d videorec < db/initdb.sql` In this example you ask postgres to connect to your local database through localhost and use username as the user.

##### Check 📝

Double-check the command you just used was successful. What should you expect to see in your local database after running this command.

### Backup your database

Now, let's create a compressed archive of your database for safekeeping. Use the `pg_dump` command with the following options:

```bash
pg_dump -h localhost -U username videorec > videorec_backup.sql.gz
```

##### Explanation

- `pg_dump`: This command is designed specifically for creating PostgreSQL database backups.
- `-h localhost` (Optional): Specify the host (`localhost` in most cases) if your PostgreSQL installation differs.
- `-U username` (Optional): Include your username if required for connection.
- `videorec`: This is the name of the database you want to back up.
- `> videorec_backup.sql.gz`:\*\* This defines the filename and format for the backup. The `>` redirects the output to a file, and `.sql.gz` indicates a gzipped SQL archive.

**Verification:** Check your terminal or file explorer to confirm the existence of the backup file (`videorec_backup.sql.gz`).

#### Stretch: Customize Backup Location

- You can modify the output filename and location to suit your preference. For example:

```bash
pg_dump -h localhost -U username videorec > backups/videorec_backup_$(date +"%Y-%m-%d").sql.gz
```

- This command incorporates the current date in the filename for easy identification and versioning.

### Removing, Re-initializing, and Restoring

Now that we have a backup, let's practice removing and re-initializing the database:

#### 1. Drop the Database

Use the `dropdb` command followed by the database name (`videorec`) to remove the database:

```bash
psql -h localhost -U username -d videorec < db/initdb.sql
dropdb videorec
```

In this example, we ask postgres to connect to your local database through `localhost` and use `username` as the user.
**Confirmation:** Verify that the database is gone by trying to connect to it with `psql -d videorec`. You should receive an error message indicating the database doesn't exist.

## Re-running the script
#### 2. Recreate the Database

It is advised to make sure that the `initdb.sql` script can be run multiple times, and each run would reset the database to a known initial state. One way to do it is to add some SQL code to the start that would delete tables in case they exist, something like:
Use the same `createdb` command from before to create a new empty database with the same name (`videorec`):

```sql
DROP TABLE IF EXISTS videos CASCADE;
```bash
createdb videorec
```

#### 3. Restore from Backup

Use `psql` with the `-f` flag to specify the backup file and restore the data into the newly created database:

```bash
psql -d videorec -f videorec_backup.sql.gz
```

Try running your `initdb.sql` script multiple times with the `psql` code above to make sure every time you get a fresh and clean database without errors.
#### 4. Verify

Connect to the database (`psql -d videorec`) to confirm the tables and data have been restored successfully.

## Sample data

Expand Down
Loading