Creating a Database in PostgreSQL

Creating a database in PostgreSQL is straightforward, but it must be performed by a user who has permissions to create databases in PostgreSQLfor example, initially the user named postgres. You can then simply issue the following command from the shell prompt (not the PSQL client prompt, but a normal shell prompt):

-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 psqi. However, you can also create databases from within psqi with the create database statement. Here's an example:

create database database;

You need to create at least one database before you can start the pgsqi 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 psqi client program with the name of the new database as a command-line argument, like so:

$ psql sampledata

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

Was this article helpful?

0 0

Post a comment