1732042446

First steps in PostgreSql


This content is for those who want to learn an alternative to Mysql or Sqlite. In this post, we'll look at the first steps to take with PostgreSql. ## <br>1. installing PostgreSQL **a) Choose your environment**: - Linux (Ubuntu/Debian): ```bash sudo apt update sudo apt install postgresql postgresql-contrib ``` - **Windows/MacOS**: Download the official PostgreSQL installer from [postgresql.org/download.](https://www.postgresql.org/download/) **b) Start the service**: - On Linux: ```bash sudo systemctl start postgresql ``` - On Windows: The service is configured automatically when installing. --- ## <br>2. Accessing PostgreSQL **a) Using the `psql` client**: - Log in as the default user: ```bash sudo -u postgres psql ``` - Create a new user: ```sql CREATE USER seu_usuario WITH PASSWORD 'your_pass'; ALTER USER seu_usuario WITH SUPERUSER; ``` **b) Graphical interfaces (optional)**: - Use tools such as pgAdmin (comes in the installer) or other GUIs such as DBeaver, DataGrip, or Beekeeper Studio. --- ## <br>3. Basic configuration **a) Change the `postgres` user password (optional)**: - Log in with `psql` and change the password: ```sql ALTER USER postgres WITH PASSWORD 'new_pass'; ``` **b) Change permissions in the `pg_hba.conf` file**: Locate the file: Usually in `/etc/postgresql/{version}/main/pg_hba.conf` or in the installation folder on Windows. Configure it to accept external connections by changing `local` or `127.0.0.1` to `0.0.0.0/0`. --- ## <br>4. Creating your first database **a) Create database**: ```sql CREATE DATABASE my_db; ``` **b) Connect to the created database**: ```bash \c my_db ``` --- ## <br>5. Basic commands - Create a table: ```sql CREATE TABLE usuarios ( id SERIAL PRIMARY KEY, nome VARCHAR(50), email VARCHAR(100), criado_em TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ``` - Enter data: ```sql INSERT INTO usuarios (nome, email) VALUES ('João', 'joao@email.com'); ``` - Consult data: ```sql SELECT * FROM usuarios; ``` --- ## <br>6. Advanced configuration (optional) **a) Enable useful extensions**: - To enable extensions such as pgcrypto (for hashes, etc.): ```sql CREATE EXTENSION pgcrypto; ``` **b) Configure backups**: - Use the `pg_dump` utility for backups: ```bash pg_dump -U postgres meu_banco > backup.sql ``` --- ## <br>7. Learning resources - Official documentation: [https://www.postgresql.org/docs/](https://www.postgresql.org/docs/) - Practice SQL commands on small databases before scaling to production. Following these steps puts you on the right path to mastering PostgreSQL!

(0) Comments

Welcome to Chat-to.dev, a space for both novice and experienced programmers to chat about programming and share code in their posts.

About | Privacy | Terms | Donate
[2024 © Chat-to.dev]