Skip to content

Instantly share code, notes, and snippets.

@CastonPursuit
Last active September 3, 2024 19:44
Show Gist options
  • Save CastonPursuit/dd5bff01cb4c404053ba06c706e0e811 to your computer and use it in GitHub Desktop.
Save CastonPursuit/dd5bff01cb4c404053ba06c706e0e811 to your computer and use it in GitHub Desktop.

🛠️ Setting Up and Ensuring Connection with PostgreSQL via TablePlus 🛠️

💡 Introduction: Before diving into data manipulation or database administration, one needs a secure and efficient platform. TablePlus provides a user-friendly interface that simplifies the interaction with PostgreSQL databases. The steps outlined below will walk you through setting up TablePlus, connecting it to a PostgreSQL database, and ensuring everything is in place for a seamless database management experience.


📚 Summary:

Access PostgreSQL: The foundational step to verify the server's operation. Database Creation: Establish a dedicated space for data, ensuring organization. Table Creation: Designate structured storage units within the database. User Management: Enhance security by creating and managing user permissions. Connect Using TablePlus: Integrate the PostgreSQL database with a graphical interface for efficient management.


  1. Access PostgreSQL: Start by accessing PostgreSQL using the superuser, usually "postgres".

    psql -U postgres

    Why? Accessing PostgreSQL ensures you have the proper permissions to operate and verifies the PostgreSQL server is active.

  2. Create the Database: Establish a new database named library.

    CREATE DATABASE library;

    Why? Databases are primary containers for data. They provide a designated space for different projects or applications.

  3. Switch to the New Database:

    \c library

    Why? Ensuring you're in the correct database avoids potential missteps, like unintentionally manipulating the wrong data set.

  4. Create a New Table: For instance, let's generate a table called authors:

    CREATE TABLE authors (
        id SERIAL PRIMARY KEY,
        name VARCHAR(100),
        birth_year INT
    );

    Why? Tables are where your data gets stored. Effective table design helps organize and handle data efficiently.

  5. Create a Less Privileged User: Now, let's create a user named librarian with restricted permissions:

    CREATE USER librarian WITH PASSWORD 'password123';

    Why? Designating specific users with limited permissions safeguards data integrity and boosts security.

  6. Grant User Access to the Database:

    GRANT CONNECT ON DATABASE library TO librarian;

    Why? This step fine-tunes user permissions, ensuring they have the precise access level needed for their tasks.

  7. Grant User Permissions on the Table:

    GRANT ALL PRIVILEGES ON TABLE authors TO librarian;

    If the table has a serial column (id in this case), ensure you also grant permissions for the sequence:

    GRANT USAGE, SELECT ON SEQUENCE authors_id_seq TO librarian;

    Why? By granting specific permissions on tables and sequences, you further narrow down user access, ensuring data safety and controlled manipulation.

  8. Exit PostgreSQL:

    \q

    Why? Exiting PostgreSQL post-operations is a basic security measure, ensuring no unintended actions occur.

  9. Connect using TablePlus:

    a. Launch TablePlus.

    b. Click on "Create a new connection" and select PostgreSQL.

    c. Fill in the connection details:
    - Name: Library Connection (or any name you prefer)
    - Host / Socket: 127.0.0.1 (for local databases)
    - Port: 5432 (default for PostgreSQL)
    - User: librarian
    - Password: password123
    - Database: library

    d. Test the connection and, if everything's correct, connect.

    Why? TablePlus offers a graphical user interface (GUI) to manage databases. It allows for an intuitive, visual method to navigate, edit, and understand database structures, making it easier for users.

@MarangelyRosas
Copy link

Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment