PostgreSQL

Definition

PostgreSQL, often referred to as Postgres, is a powerful, open-source relational database system. It is known for its reliability and extensive feature set, making it a popular choice for various applications. PostgreSQL is an object-relational database system that uses and extends the SQL language, providing a robust platform for managing data.

Usage in Applications

In the context of this book, PostgreSQL is used to demonstrate connecting and querying a database using Rust and the sqlx crate. It serves as the database for storing and retrieving course information in the tutor web service, specifically within the EzyTutors application. PostgreSQL is deployed as a Docker container to manage the application's data efficiently.

Installation and Configuration

PostgreSQL can be installed locally, run in a Docker container, or accessed as a managed service in the cloud. Below are the steps to install PostgreSQL on a Linux Ubuntu server:

  1. Install PostgreSQL and Contrib Package:

    To install the PostgreSQL package along with a contrib package that includes additional utilities, use the following command:

    sudo apt install postgresql postgresql-contrib
    

    The installation process automatically starts the PostgreSQL server as a systemd process in Linux.

  2. Verify PostgreSQL Server:

    To verify that the PostgreSQL server is running, execute the following command:

    ps aux | grep postgres
    

    This command should display the Postgres processes running in the background.

  3. Interact with PostgreSQL:

    PostgreSQL uses the concept of a role for authentication and authorization, similar to a user in Linux/Unix. The installation process creates a user account called postgres. To log into this account, use:

    sudo -i -u postgres
    

    Once logged in, you can access the Postgres shell prompt to perform tasks such as creating databases and users.

  4. Restart PostgreSQL Server:

    After making configuration changes, restart the PostgreSQL server with the following command:

    sudo systemctl restart postgresql
    

    This ensures that any configuration changes are applied, allowing you to log in to a Postgres database with a password once logged in to the server.

Listing Databases

To list PostgreSQL databases, use the \list command in the psql shell. This command provides a list of all databases in the PostgreSQL instance, including the ezytutors database. This is a useful command for managing and navigating through different databases within the PostgreSQL environment.

FAQ (Frequently asked questions)

How is PostgreSQL used in the book 'Rust Servers, Services, and Apps'?

How is PostgreSQL utilized in the tutor web service?