Database Cluster
A database cluster is a collection of databases that is managed by a single instance of a running database server. In this post, we will talk about how to create a database cluster.
Prerequisites
The data created by postgresql is stored on the host file system in a
directory called data directory. You can choose any directory to be
data directory for example: /usr/local/pgsql/data
or
/var/lib/pgsql/data
. The data directory is initialized using a program
called initdb
which comes with standard postgresql-server
installation.
Installing postgresql-server
Almost all linux systems provide a package called postgresql-server
or
a package with similar name. You can check the exact name by searching
package repository for your distribution. For fedora, it is
postgresql-server
which can be installed by running:
|
|
Initializing the data directory
The data directory can be intialized using initdb program. But before running the command, there are a few important points to remember:
- The data must be owned by a particular
user
that is notroot
. Therefore it is advisable to create a new user. The postgresql installation automatically creates a user calledpostgres
which can be used for the purpose. - The data directory must be owned by the user you create. And all
commands to control the server i.e.
pg_ctl
commands, must be issued using the special user you create.
For this blog post, we will assume that you are going to use postgres
user created by default, and use the directory /usr/local/psql/data
as
data directory.
Creating the data directory
Initially, the postgres
user will not have permission to create a
directory. Therefore, you first need to create the data directory and
then change it’s owner to postgres
|
|
After this, initialize the data directory using:
|
|
Start the server
Now you are all set. Start the server using:
|
|
Please take special care that all commands must be issued by the
postgres
user, using sudo -u postgres [command]
.
Testing and Using postgresql (Optional)
After you have successfully fired up the postgresql server using the above instructions. You might want to test it.
For the purpose of using the postgresql-server
you need a postgresql
client. You can use
psql
, which
is a terminal client for interacting with postgresql server. Most
distributions provide a plain postgersql
package which contains this
binary. On fedora, simply run:
|
|
This will give you the psql command.
sh man psql
After, installing psql
, now you need to log into your postgres
user
account and start psql
. It is also recommended that you change the
password for the postgres
user as per your liking.
|
|
Then simply log into the postgres
user account.
|
|
You are now logged into the psql
shell. Use \l
to see available
databases.
|
|
By default, you get these 3 databases. You can run queries and play with the data.