1
0
mirror of https://github.com/actix/examples synced 2024-12-18 00:13:57 +01:00
examples/databases/postgres
dependabot[bot] 7146f8e31c
Bump deadpool-postgres from 0.11.0 to 0.12.1
Bumps [deadpool-postgres](https://github.com/bikeshedder/deadpool) from 0.11.0 to 0.12.1.
- [Changelog](https://github.com/bikeshedder/deadpool/blob/master/CHANGELOG.md)
- [Commits](https://github.com/bikeshedder/deadpool/compare/deadpool-postgres-v0.11.0...deadpool-postgres-v0.12.1)

---
updated-dependencies:
- dependency-name: deadpool-postgres
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-12-25 17:50:22 +00:00
..
sql restructure folders 2022-02-18 02:01:48 +00:00
src update dotenv::dotenv references 2023-10-29 01:18:40 +01:00
.gitignore restructure folders 2022-02-18 02:01:48 +00:00
Cargo.toml Bump deadpool-postgres from 0.11.0 to 0.12.1 2023-12-25 17:50:22 +00:00
README.md Update databases/postgres/README.md (#554) 2022-06-06 19:20:46 +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

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

    createuser -P test_user
    

    Enter a password of your choice. The following instructions assume you used testing as password.

    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:

    CREATE USER test_user WITH PASSWORD 'testing';
    
  2. Create database

    createdb -O test_user testing_db
    

    An alternative using SQL:

    CREATE DATABASE testing_db OWNER test_user;
    
  3. Initialize database

    psql -f sql/schema.sql testing_db
    

    This step can be repeated and clears the database as it drops and recreates the schema testing which is used within the database.

  4. Grant privileges to new user

    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:

    SERVER_ADDR=127.0.0.1:8080
    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
    
  6. Run the server:

    cargo run
    
  7. Using a different terminal send an HTTP POST request to the running server:

    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
    

    ...or using curl...

    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).