$ sudo su - postgres
or
$ sudo -i -u postgres
then
$ psql
$ sudo -u postgres psql
or
$ psql -U my_db_user
postgres=# \q
$ psql -U my_db_user
then
postgres=# \c my_db
or
$ psql -U my_db_user -d my_db
$ sudo service postgresql restart
postgres=# select * from pg_roles;
postgres=# CREATE ROLE my_demo_role WITH CREATEDB LOGIN ENCRYPTED PASSWORD 'my_password';
Notes about privilegies:
-
SUPERUSER - A database SUPERUSER deserves a word of caution. Bottom line, roles with this attribute can create another SUPERUSER. Matter of fact, this attribute is required to create another SUPERUSER role. Since roles with this attribute bypass all permission checks, grant this privilege judiciously.
-
CREATEDB - Allows the role to create databases.
-
CREATEROLE - With this attribute, a role can issue the CREATE ROLE command. Hence, create other roles.
-
LOGIN - Enables the ability to login. A role name with this attribute can be used in the client connection command. More info about privilegies here... More info about roles here...
postgres=# DROP ROLE demo_role;
postgres=# ALTER USER engenhoapp_dev WITH CREATEDB LOGIN ENCRYPTED PASSWORD ‘fe375500’';
postgres=# CREATE DATABASE db_demo;
postgres=# \c db_demo
postgres=# \q
postgres=# ALTER DATABASE db_to_change RENAME TO new_name;
To rename a database, you have to connect to another database e.g., postgres.
postgres=# ALTER DATABASE db_to_change OWNER TO new_onwer;
Only the superuseror owner of the database can change the database’s owner. The database owner must also have the CREATEDB privilege to rename the database.
my_database -c 'SHOW SERVER_ENCODING';
To change the default tablespace of the database, you use ALTER DATABASE SET TABLESPACE as follows:
postgres=# ALTER DATABASE db_name SET TABLESPACE new_tablespace;
The statement moves tables and indexes from the legacy tablespace to the new one.
postgres=#
CREATE TABLE users (
id SERIAL PRIMARY KEY,
age INT,
first_name TEXT,
last_name TEXT,
email TEXT UNIQUE NOT NULL
);postgres=#
SELECT d.datname as "Name",
pg_catalog.pg_get_userbyid(d.datdba) as "Owner"
FROM pg_catalog.pg_database d
WHERE d.datname = 'your_name'
ORDER BY 1;