Skip to content

Instantly share code, notes, and snippets.

@Taher1307
Forked from phortuin/postgres.md
Created June 15, 2022 12:04
Show Gist options
  • Save Taher1307/7fc83709fe038d020952f07b19f421be to your computer and use it in GitHub Desktop.
Save Taher1307/7fc83709fe038d020952f07b19f421be to your computer and use it in GitHub Desktop.

Revisions

  1. @phortuin phortuin revised this gist Oct 5, 2021. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion postgres.md
    Original file line number Diff line number Diff line change
    @@ -70,7 +70,13 @@ postgres-> \c mydatabase
    mydatabase-> \dt
    ```

    ...should print `Did not find any relations.` for an empty database. Finally, in a `.env` file for Node.js software development, your database connection string should look like this:
    ...should print `Did not find any relations.` for an empty database. To quit the postgres CLI:

    ```
    mydatabase-> \q
    ```

    Finally, in a `.env` file for Node.js software development, your database connection string should look like this:

    ```
    PG_CONNECTION_STRING=postgres://myuser@localhost/mydatabase
  2. @phortuin phortuin revised this gist Sep 21, 2021. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -23,8 +23,8 @@ $ psql postgres
    Create role for application, give login and `CREATEDB` permissions:

    ```postgres
    postgres-# CREATE ROLE myapp WITH LOGIN;
    postgres-# ALTER ROLE myapp CREATEDB;
    postgres-# CREATE ROLE myuser WITH LOGIN;
    postgres-# ALTER ROLE myuser CREATEDB;
    ```

    Note that the user has no password. Listing users `\du` should look like this:
    @@ -35,7 +35,7 @@ postgres-# \du
    Role name | Attributes | Member of
    -------------+------------------------------------------------------------+-----------
    <root user> | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
    myapp | Create DB | {}
    myuser | Create DB | {}
    ```

    Quit psql, because we will login with the new role (=user) to create a database:
    @@ -44,17 +44,17 @@ Quit psql, because we will login with the new role (=user) to create a database:
    postgres-# \q
    ```

    On shell, open psql with `postgres` database with user `myapp`:
    On shell, open psql with `postgres` database with user `myuser`:

    ```bash
    $ psql postgres -U myapp
    $ psql postgres -U myuser
    ```

    Note that the postgres prompt looks different, because we’re not logged in as a root user anymore. We’ll create a database and grant all privileges to our user:

    ```postgres
    postgres-> CREATE DATABASE myapp;
    postgres-> GRANT ALL PRIVILEGES ON DATABASE myapp TO myapp;
    postgres-> CREATE DATABASE mydatabase;
    postgres-> GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;
    ```

    List databases to verify:
    @@ -66,12 +66,12 @@ postgres-> \list
    If you want to connect to a database and list all tables:

    ```postgres
    postgres-> \c myapp
    myapp-> \dt
    postgres-> \c mydatabase
    mydatabase-> \dt
    ```

    ...should print `Did not find any relations.` for an empty database. Finally, in a `.env` file for Node.js software development, your database connection string should look like this:

    ```
    PG_CONNECTION_STRING=postgres://myapp@localhost/myapp
    PG_CONNECTION_STRING=postgres://myuser@localhost/mydatabase
    ```
  3. @phortuin phortuin revised this gist Sep 20, 2021. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -70,9 +70,7 @@ postgres-> \c myapp
    myapp-> \dt
    ```

    ...should print `Did not find any relations.` for an empty database.

    In a `.env` file for Node.js software development, your database connection string should look like this.
    ...should print `Did not find any relations.` for an empty database. Finally, in a `.env` file for Node.js software development, your database connection string should look like this:

    ```
    PG_CONNECTION_STRING=postgres://myapp@localhost/myapp
  4. @phortuin phortuin revised this gist Sep 20, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion postgres.md
    Original file line number Diff line number Diff line change
    @@ -72,7 +72,7 @@ myapp-> \dt

    ...should print `Did not find any relations.` for an empty database.

    Your connection string should look like this:
    In a `.env` file for Node.js software development, your database connection string should look like this.

    ```
    PG_CONNECTION_STRING=postgres://myapp@localhost/myapp
  5. @phortuin phortuin revised this gist Sep 20, 2021. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion postgres.md
    Original file line number Diff line number Diff line change
    @@ -70,4 +70,10 @@ postgres-> \c myapp
    myapp-> \dt
    ```

    ...should print `Did not find any relations.` for an empty database.
    ...should print `Did not find any relations.` for an empty database.

    Your connection string should look like this:

    ```
    PG_CONNECTION_STRING=postgres://myapp@localhost/myapp
    ```
  6. @phortuin phortuin revised this gist Jul 4, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion postgres.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ $ brew install postgresql
    Run server:

    ```bash
    $ pg_ctl -D /opt/homebrew/var/postgres start
    $ pg_ctl -D /opt/homebrew/var/postgres start
    ```

    Note: if you’re on Intel, the `/opt/homebrew` probably is `/usr/local`.
  7. @phortuin phortuin revised this gist Jul 4, 2021. 1 changed file with 23 additions and 1 deletion.
    24 changes: 23 additions & 1 deletion postgres.md
    Original file line number Diff line number Diff line change
    @@ -48,4 +48,26 @@ On shell, open psql with `postgres` database with user `myapp`:

    ```bash
    $ psql postgres -U myapp
    ```
    ```

    Note that the postgres prompt looks different, because we’re not logged in as a root user anymore. We’ll create a database and grant all privileges to our user:

    ```postgres
    postgres-> CREATE DATABASE myapp;
    postgres-> GRANT ALL PRIVILEGES ON DATABASE myapp TO myapp;
    ```

    List databases to verify:

    ```postgres
    postgres-> \list
    ```

    If you want to connect to a database and list all tables:

    ```postgres
    postgres-> \c myapp
    myapp-> \dt
    ```

    ...should print `Did not find any relations.` for an empty database.
  8. @phortuin phortuin revised this gist Jul 4, 2021. 1 changed file with 14 additions and 2 deletions.
    16 changes: 14 additions & 2 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ $ pg_ctl -D /opt/homebrew/var/postgres start

    Note: if you’re on Intel, the `/opt/homebrew` probably is `/usr/local`.

    Start psql and open database `postgres`:
    Start psql and open database `postgres`, which is the database postgres uses itself to store roles, permissions, and structure:

    ```bash
    $ psql postgres
    @@ -35,5 +35,17 @@ postgres-# \du
    Role name | Attributes | Member of
    -------------+------------------------------------------------------------+-----------
    <root user> | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
    myapp. | Create DB | {}
    myapp | Create DB | {}
    ```

    Quit psql, because we will login with the new role (=user) to create a database:

    ```postgres
    postgres-# \q
    ```

    On shell, open psql with `postgres` database with user `myapp`:

    ```bash
    $ psql postgres -U myapp
    ```
  9. @phortuin phortuin revised this gist Jul 4, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,8 @@ Run server:
    $ pg_ctl -D /opt/homebrew/var/postgres start
    ```

    Note: if you’re on Intel, the `/opt/homebrew` probably is `/usr/local`.

    Start psql and open database `postgres`:

    ```bash
  10. @phortuin phortuin revised this gist Jul 4, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ Create role for application, give login and `CREATEDB` permissions:

    ```postgres
    postgres-# CREATE ROLE myapp WITH LOGIN;
    postgres-# ALTER ROLE traxcal CREATEDB;
    postgres-# ALTER ROLE myapp CREATEDB;
    ```

    Note that the user has no password. Listing users `\du` should look like this:
    @@ -33,5 +33,5 @@ postgres-# \du
    Role name | Attributes | Member of
    -------------+------------------------------------------------------------+-----------
    <root user> | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
    traxcal | Create DB | {}
    myapp. | Create DB | {}
    ```
  11. @phortuin phortuin revised this gist Jul 4, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion postgres.md
    Original file line number Diff line number Diff line change
    @@ -32,6 +32,6 @@ postgres-# \du
    List of roles
    Role name | Attributes | Member of
    -------------+------------------------------------------------------------+-----------
    (root user) | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
    <root user> | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
    traxcal | Create DB | {}
    ```
  12. @phortuin phortuin revised this gist Jul 4, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion postgres.md
    Original file line number Diff line number Diff line change
    @@ -33,5 +33,5 @@ postgres-# \du
    Role name | Attributes | Member of
    -------------+------------------------------------------------------------+-----------
    (root user) | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
    myapp | Create DB. | {}
    traxcal | Create DB | {}
    ```
  13. @phortuin phortuin revised this gist Jul 4, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -21,11 +21,11 @@ $ psql postgres
    Create role for application, give login and `CREATEDB` permissions:

    ```postgres
    postgres-# CREATE ROLE myapp WITH LOGIN PASSWORD '<password>';
    postgres-# CREATE ROLE myapp WITH LOGIN;
    postgres-# ALTER ROLE traxcal CREATEDB;
    ```

    User should look like this:
    Note that the user has no password. Listing users `\du` should look like this:

    ```postgres
    postgres-# \du
  14. @phortuin phortuin revised this gist Jul 4, 2021. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -18,10 +18,10 @@ Start psql and open database `postgres`:
    $ psql postgres
    ```

    Create role for application and give rights (the semicolon at the end is important):
    Create role for application, give login and `CREATEDB` permissions:

    ```postgres
    postgres-# CREATE ROLE myapp;
    postgres-# CREATE ROLE myapp WITH LOGIN PASSWORD '<password>';
    postgres-# ALTER ROLE traxcal CREATEDB;
    ```

    @@ -33,5 +33,5 @@ postgres-# \du
    Role name | Attributes | Member of
    -------------+------------------------------------------------------------+-----------
    (root user) | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
    myapp | Create DB, Cannot login | {}
    myapp | Create DB. | {}
    ```
  15. @phortuin phortuin revised this gist Jul 4, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion postgres.md
    Original file line number Diff line number Diff line change
    @@ -33,5 +33,5 @@ postgres-# \du
    Role name | Attributes | Member of
    -------------+------------------------------------------------------------+-----------
    (root user) | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
    traxcal | Create DB, Cannot login | {}
    myapp | Create DB, Cannot login | {}
    ```
  16. @phortuin phortuin revised this gist Jul 4, 2021. 1 changed file with 12 additions and 2 deletions.
    14 changes: 12 additions & 2 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,16 @@ Create role for application and give rights (the semicolon at the end is importa

    ```postgres
    postgres-# CREATE ROLE myapp;
    # CREATE ROLE
    postgres-# ALTER ROLE traxcal CREATEDB
    postgres-# ALTER ROLE traxcal CREATEDB;
    ```

    User should look like this:

    ```postgres
    postgres-# \du
    List of roles
    Role name | Attributes | Member of
    -------------+------------------------------------------------------------+-----------
    (root user) | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
    traxcal | Create DB, Cannot login | {}
    ```
  17. @phortuin phortuin revised this gist Jul 4, 2021. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -12,15 +12,16 @@ Run server:
    $ pg_ctl -D /opt/homebrew/var/postgres start
    ```

    Start psql with default user `postgres`:
    Start psql and open database `postgres`:

    ```bash
    $ psql postgres
    ```

    Create role for application and give rights:
    Create role for application and give rights (the semicolon at the end is important):

    ```postgres
    posgres-# CREATE ROLE myapp
    postgres-# CREATE ROLE myapp;
    # CREATE ROLE
    postgres-# ALTER ROLE traxcal CREATEDB
    ```
  18. @phortuin phortuin revised this gist Jul 4, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion postgres.md
    Original file line number Diff line number Diff line change
    @@ -18,8 +18,9 @@ Start psql with default user `postgres`:
    $ psql postgres
    ```

    Create role for application:
    Create role for application and give rights:

    ```postgres
    posgres-# CREATE ROLE myapp
    postgres-# ALTER ROLE traxcal CREATEDB
    ```
  19. @phortuin phortuin revised this gist Jul 4, 2021. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -17,3 +17,9 @@ Start psql with default user `postgres`:
    ```bash
    $ psql postgres
    ```

    Create role for application:

    ```postgres
    posgres-# CREATE ROLE myapp
    ```
  20. @phortuin phortuin created this gist Jul 4, 2021.
    19 changes: 19 additions & 0 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    Based on [this blogpost](https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb).

    Install with Homebrew:

    ```bash
    $ brew install postgresql
    ```

    Run server:

    ```bash
    $ pg_ctl -D /opt/homebrew/var/postgres start
    ```

    Start psql with default user `postgres`:

    ```bash
    $ psql postgres
    ```