Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Manage Databases on macOS Using PostgreSQL

Database management is a critical skill for developers and system administrators. On macOS, PostgreSQL is a popular choice for relational database management due to its robustness, scalability, and open-source nature. This article will guide you through the process of installing, configuring, and managing PostgreSQL databases on a macOS environment. The instructions are tailored to leverage macOS-specific tools and commands.

Examples:

  1. Installing PostgreSQL on macOS: To install PostgreSQL, you can use Homebrew, a package manager for macOS. Open Terminal and execute the following command:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    brew install postgresql
  2. Starting and Stopping PostgreSQL Service: After installation, you can start the PostgreSQL service using:

    brew services start postgresql

    To stop the service, use:

    brew services stop postgresql
  3. Creating a New Database: Once PostgreSQL is running, you can create a new database by first switching to the PostgreSQL user and then using the createdb command:

    sudo -i -u postgres
    createdb mydatabase
  4. Accessing the PostgreSQL Shell: To interact with your database, you can use the psql command-line tool:

    psql mydatabase

    This will open the PostgreSQL shell where you can run SQL commands.

  5. Creating a Table and Inserting Data: Within the psql shell, you can create a table and insert data as follows:

    CREATE TABLE users (
       id SERIAL PRIMARY KEY,
       name VARCHAR(100),
       email VARCHAR(100) UNIQUE
    );
    
    INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com');
  6. Querying Data: To retrieve data from the table, you can run:

    SELECT * FROM users;
  7. Backing Up and Restoring Databases: To back up a database, use the pg_dump command:

    pg_dump mydatabase > mydatabase_backup.sql

    To restore a database from a backup, use the psql command:

    psql mydatabase < mydatabase_backup.sql

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.