Start postgresql on Ubuntu

1. Install

Step 1: Run command to update system packages:

<pre v-pre data-lang="bash"><code class="lang-bash">sudo apt-get update &amp;&amp; sudo apt-get upgrade</code></pre>

Step 2: Run command to install postgresql

<pre v-pre data-lang="bash"><code class="lang-bash">sudo apt-get install postgresql</code></pre>

2. Enable remote access

You might need to update pg_hba.conf and postgresql.conf for the client authentication and listener address in order to allow remote access to the postgres database.

3. Run psql

You can use the following command to start use psql

  1. Switch to postgres user: sudo su - postgres
  2. Run sql with psql: psql -c "sql command"

4. Manage databases

  1. Create database: CREATE DATABASE mydb
  2. Drop database: DROP DATABASE mydb
  3. Grant permission: GRANT ALL PRIVILEGES ON DATABASE mydb TO myrole/user

5. Manage roles

  1. Create role: CREATE ROLE myrole
  2. Drop role: DROP ROLE myrole
  3. Grant permission GRANT group_role TO role1, role2....
  4. Revoke permission REVOKE group_role FROM role1, role2...

6. Manage users

  1. Create user: CREATE USER myuser WITH ENCRYPTED PASSWORD 'password'
  2. Drop user: DROP USER myuser

7. References

  1. Postgres Documentation - https://www.postgresql.org/docs/current/index.html