PostgreSQL Module #59 Flashcards
What type of database is PostgreSQL?
It is a relational database. Think TABLES :-)
What command needs to be entered on the command i line in terminal to get access to the database?
psql postgres (which gives access to the postgres db created by default)
How are new databases added to Postgre ?
By calling the CREATE DATABASE test; command. Don’t forget the semicolon as it is necessary.
What’s the syntax to create a new table in our DB?
CREATE TABLE users {
id SERIAL PRIMARY KEY,
email VARCHAR(255) NOT NULL
);
How can you connect to a specific database
By typing
\c test or the name of the db
What’s the syntax for listing your databases (and templates) in PostgreSQL?
\list or \l
How are templates and databases delineated in PostgreSQL?
Databases take the name you assign and templates are called templates which are numbers.
template1
template2 etc
What are templates in PostgreSQL? How are they created?
Templates are predefined databases. You can use them to pre-populate new databases and you use the syntax to generate them:
CREATE DATABASE databasename Template template0
How can we view a more detailed overview of our databases showing the disc sizes for each?
\list+ or +
How are new users created in PostgreSQL?
There are no users, just roles.
How are roles created in PostgreSQL?
On MacOS, you’re automatically assigned a role when you run psql postgres.
How can you view roles?
By typing: \du
How would you create a new role?
Use this syntax:
CREATE ROLE ; a better example is
CREATE ROLE testing;
But this example creates a user without login rights.
How are Roles with Login capabilities created?
CREATE ROLE rolename WITH LOGIN;
How can users login under their ROLE?
Use this syntax:
psql postgres -U testing rolename
From terminal how can you quickly tell the difference between a superuser and a normal role?
The prompt changes from =# to =>
How can we password protect a role?
CREATE ROLE WITH LOGIN PASSWORD ‘’;
How are roles altered?
ALTER ROLE WITH LOGIN;
What are the other built-in role attributes?
LOGIN / NOLOGIN: allow (or not) to login to Postgres
SUPERUSER / NOSUPERUSER: allow (or not) superuser permissions. A database superuser will bypass other permission checks, except for LOGIN (it must be granted separately).
CREATEDB / NOCREATEDB: allow (or not) the ability to create new databases
CREATEROLE / NOCREATEROLE: allow (or not) the ability to create new roles
CREATEUSER / NOCREATEUSER: allow (or not) the ability to create new users
INHERIT / NOINHERIT: allow (or not) the ability to make the privileges inheritable
REPLICATION / NOREPLICATION: grant (or not) replication permissions (an advanced topic we’ll not cover)
What’s an alternate way to create a user / role with a password. TIP: This method is less steps than the others?
CREATE USER rolename PASSWORD ‘password’;
Just like there really aren’t users in PostgreSQL there aren’t groups either. How are group roles created?
CREATE ROLE groupname;
Once the group role is created access to that group needs to be granted, how is that done?
Use this syntax:
GRANT TO
For example, we can create a flavio user role, a “employee” group role, and assign the user to the group role:
CREATE USER flavio PASSWORD ‘superSecret123$’;
CREATE ROLE employee;
GRANT employee TO flavio;
How are group roles removed?
REVOKE FROM
Example:
REVOKE employee FROM flavio;
Can roles get group attributes once they’re added to the group?
No, by default, adding a role to a group role will not make the role inherit attributes (permissions) from the group role.
You need to create the group role with the INHERIT attribute.
How can you create a role that inherits group attributes?
CREATE ROLE employee WITH CREATEDB INHERIT;
The next time you grant a role to a a role that’s in a group, it will INHERIT those group attributes.
CREATE ROLE flavio;
GRANT employee TO flavio;
What is one of the easiest Cloud providers for postgreSQL?
Heroku. https://www.heroku.com/
Heroku for example provides a built-in managed PostgreSQL cloud database as a service, which might be very convenient instead of managing your own database server
Extra Credit: what is the fully open source fork of MySQL from Oracle called?
MariaDB
Define ACID?
Atomicity, Conssistency, Isolation, Durability