1
0
mirror of https://github.com/actix/examples synced 2024-11-23 14:31:07 +01:00

Update databases/postgres/README.md (#554)

* Update databases/postgres/README.md

Add sudo -u postgres to relevant commands

Add privilege granting step for new user

Add -i flag to curl command to show HTTP response code

* Update databases/postgres/README.md

Add more options for accomplishing tasks

Add note regarding
This commit is contained in:
Andrew Langmeier 2022-06-06 14:20:46 -04:00 committed by GitHub
parent b459225d5a
commit a7a7f0858b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,6 +9,11 @@
## Instructions
### NOTE:
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`
1. Create database user
```shell
@ -19,12 +24,22 @@
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.
An alternative using SQL:
```sql
CREATE USER test_user WITH PASSWORD 'testing';
```
2. Create database
```shell
createdb -O test_user testing_db
```
An alternative using SQL:
```sql
CREATE DATABASE testing_db OWNER test_user;
```
3. Initialize database
```shell
@ -33,7 +48,15 @@
This step can be repeated and clears the database as it drops and recreates the schema `testing` which is used within the database.
4. Create `.env` file:
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;
```
5. Create `.env` file:
```ini
SERVER_ADDR=127.0.0.1:8080
@ -45,13 +68,13 @@
PG.POOL.MAX_SIZE=16
```
5. Run the server:
6. Run the server:
```shell
cargo run
```
6. Using a different terminal send an HTTP POST request to the running server:
7. Using a different terminal send an HTTP POST request to the running server:
```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
@ -60,7 +83,7 @@
**...or using curl...**
```shell
curl -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
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
```
A unique constraint exists for username, so sending this request twice will return an internal server error (HTTP 500).