Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

issue the following command from the shell prompt (not the PSQL client
prompt, but a normal shell prompt):


Click here to view code image
matthew@seymour:~# su - postgres
-bash-2.05b$ createdb database


where database is the name of the database you want to create.


The createdb program is actually a wrapper that makes it easier to create
databases without having to log in and use psql. However, you can also
create databases from within psql with the CREATE DATABASE statement.
Here is an example:


Click here to view code image
CREATE DATABASE database;


You need to create at least one database before you can start the psql client
program. You should create this database while you’re logged in as the user
postgres. To log in as this user, you need to use su to become root and
then use su to become the user postgres. To connect to the new database,
you start the psql client program with the name of the new database as a
command-line argument, like this:


Click here to view code image
matthew@seymour:~$ psql sampledata


If you don’t specify the name of a database when you invoke psql, the
command attempts to connect to a database that has the same name as the
user as which you invoke psql (that is, the default database).


Creating Database Users in PostgreSQL


To create a database user, you use su to become the user postgres from


the Linux root account. You can then use the PostgreSQL createuser
command to quickly create a user who is allowed to access databases or
create new database users, as follows:


Click here to view code image
matthew@seymour:~$ createuser sandra
Shall the new user be allowed to create databases? (y/n) y
Shall the new user be allowed to create more new users? (y/n) y
CREATE USER


In this example, the new user named sandra is created and allowed to create
new databases and database users. (Carefully consider who is allowed to

Free download pdf