2020-01-16 18:41:45 +01:00
# async_pg example
## This example illustrates
- `tokio_postgres`
- use of `tokio_pg_mapper` for postgres data mapping
- `deadpool_postgres` for connection pooling
- `dotenv` + `config` for configuration
## Instructions
2022-06-06 20:20:46 +02:00
### NOTE:
2024-08-07 03:04:57 +02:00
You may need to ensure that you are running the commands with the correct SQL user. On many Linux distributions you may prefix the shell commands with `sudo -u postgres`
2022-06-06 20:20:46 +02:00
2020-01-16 19:21:17 +01:00
1. Create database user
2020-01-16 18:41:45 +01:00
2022-03-06 01:43:10 +01:00
```shell
createuser -P test_user
```
2020-01-16 18:41:45 +01:00
2022-03-06 01:43:10 +01:00
Enter a password of your choice. The following instructions assume you used `testing` as password.
2020-01-16 19:21:17 +01:00
2022-03-06 01:43:10 +01:00
This step is **optional** and you can also use an existing database user for that. Just make sure to replace `test_user` by the database user of your choice in the following steps and change the `.env` file containing the configuration accordingly.
2020-01-16 19:21:17 +01:00
2022-06-06 20:20:46 +02:00
An alternative using SQL:
2024-08-07 03:04:57 +02:00
2022-06-06 20:20:46 +02:00
```sql
CREATE USER test_user WITH PASSWORD 'testing';
```
2020-01-16 19:21:17 +01:00
2. Create database
2022-03-06 01:43:10 +01:00
```shell
createdb -O test_user testing_db
```
2020-01-16 19:21:17 +01:00
2022-06-06 20:20:46 +02:00
An alternative using SQL:
2024-08-07 03:04:57 +02:00
2022-06-06 20:20:46 +02:00
```sql
CREATE DATABASE testing_db OWNER test_user;
```
2020-01-16 19:21:17 +01:00
3. Initialize database
2022-03-06 01:43:10 +01:00
```shell
psql -f sql/schema.sql testing_db
```
2020-01-16 19:21:17 +01:00
2022-03-06 01:43:10 +01:00
This step can be repeated and clears the database as it drops and recreates the schema `testing` which is used within the database.
2020-01-16 19:21:17 +01:00
2022-06-06 20:20:46 +02:00
4. Grant privileges to new user
```sql
GRANT ALL PRIVILEGES ON SCHEMA testing TO test_user;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA testing TO test_user;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA testing TO test_user;
2024-08-07 03:04:57 +02:00
```
2022-06-06 20:20:46 +02:00
5. Create `.env` file:
2020-01-16 18:41:45 +01:00
2022-03-06 01:43:10 +01:00
```ini
SERVER_ADDR=127.0.0.1:8080
2024-07-28 21:22:23 +02:00
PG__USER=test_user
PG__PASSWORD=testing
PG__HOST=127.0.0.1
PG__PORT=5432
PG__DBNAME=testing_db
PG__POOL_MAX_SIZE=16
2022-03-06 01:43:10 +01:00
```
2020-01-16 18:41:45 +01:00
2022-06-06 20:20:46 +02:00
6. Run the server:
2020-01-16 18:41:45 +01:00
2022-03-06 01:43:10 +01:00
```shell
cargo run
```
2020-01-16 18:41:45 +01:00
2022-06-06 20:20:46 +02:00
7. Using a different terminal send an HTTP POST request to the running server:
2020-01-16 18:41:45 +01:00
2022-03-06 01:43:10 +01:00
```shell
echo '{"email": "ferris@thecrab.com", "first_name": "ferris", "last_name": "crab", "username": "ferreal"}' | http -f --json --print h POST http://127.0.0.1:8080/users
```
2020-01-16 18:41:45 +01:00
2022-03-06 01:43:10 +01:00
** ...or using curl...**
2020-01-16 19:21:17 +01:00
2022-03-06 01:43:10 +01:00
```shell
2022-06-06 20:20:46 +02:00
curl -i -d '{"email": "ferris@thecrab.com", "first_name": "ferris", "last_name": "crab", "username": "ferreal"}' -H 'Content-Type: application/json' http://127.0.0.1:8080/users
2022-03-06 01:43:10 +01:00
```
2020-01-16 19:21:17 +01:00
2022-03-06 01:43:10 +01:00
A unique constraint exists for username, so sending this request twice will return an internal server error (HTTP 500).