Background Light
How to check Your postgresql in Python

How to check Your postgresql in Python

To check the status of your PostgreSQL server and inspect your databases and users, you'll use a combination of system commands and PostgreSQL's own command-line client (psql).

Here's how to check your PostgreSQL setup:

1. Check PostgreSQL Service Status

First, verify that the PostgreSQL service is running on your Ubuntu server.

Bash

sudo systemctl status postgresql

Expected Output:

You should see Active: active (exited) (which is normal for PostgreSQL's systemd unit, as it manages the background processes) or Active: active (running). The key is that it's active and not inactive or failed.

If it's not active, you can start it with:

Bash

sudo systemctl start postgresql

And enable it to start on boot:

Bash

sudo systemctl enable postgresql
 

2. Log In to PostgreSQL as the postgres User

The postgres user is the default superuser for the PostgreSQL database system. You'll need to switch to this system user to interact with the database.

Bash

sudo -i -u postgres

Your terminal prompt should change, indicating you are now the postgres user (e.g., postgres@yourserver:~).

3. Enter the psql interactive terminal

Once you are the postgres system user, you can launch the PostgreSQL command-line client.

Bash

psql

Your prompt will now change to postgres=# (or similar), indicating you are inside the psql interactive terminal.

4. Check Databases and Users within psql

Now that you're in the psql prompt, you can run SQL-like meta-commands (starting with \) to inspect the database.

  • List all databases:
    SQL
    \l

    or
    SQL
    \list

    Look for eyewebmaster_blog_db in the list.
  • List all users (roles):
    SQL
    \du

    or
    SQL
    \dp

    Look for eyewebmaster_db_user and verify its attributes.
  • Connect to your specific database (optional):
    You can connect to your newly created database to ensure you can access it.
    SQL
    \c eyewebmaster_blog_db

    The prompt should change to eyewebmaster_blog_db=#.
  • Check tables in your database (if you've run migrations):
    If you've already run python manage.py migrate, you can check if tables were created.
    SQL
    \dt

    You should see Django's default tables (e.g., auth_user, django_session, blog_post, etc.).

5. Exit psql and postgres user

  • Exit psql:
    SQL
    \q

     
  • Exit postgres user (return to your normal user):
    Bash
    exit

     

By following these steps, you can thoroughly check if PostgreSQL is running, and if your database and user have been created correctly and are accessible.