Crash course in PostgreSQL, part 1

Get started with the world's most advanced open source database.

By , ITworld |  Data Center, crash course, database


Creating and destroying a new PostgreSQL database

You'll have to jump through a number of startup and user account hurdles to get started. First, verify that your PostgreSQL server has started by opening psql, its interactive command shell:

$ psql
psql: could not connect to server: No such file or directory

Oops, PostgreSQL is not running. Go back to the appropriate installation guide to see how to start it on your system. On Debian, it starts automatically after installation, so the above command produces the following result:

carla@xena:~$ psql
psql: FATAL:  role "carla" does not exist

Well, excuse me all to heck! But this really isn't a big deal, because PostgreSQL creates a default postgres superuser with no password. So you have to change to this user, and then create a new user account that you will use to administer the database. On Linux and Unix you need to gain rootly powers, like this:

carla@xena:~$ su
root@xena:/home/carla# su postgres
postgres@xena:/home/carla$

There, now we can get some real work done! Let's create a carla superuser for PostgreSQL:

postgres@xena:/home/carla$ createuser carla
could not change directory to "/home/carla"
Shall the new role be a superuser? (y/n) y

These PostgreSQL roles, postgres and carla, are PostgreSQL user accounts that are independent of system accounts. A role can be a single user or a group of users. Roles can own database objects, such as tables, and can assign privileges to access those tables to other roles. Use the dropuser command to delete a role:

$ dropuser carla

Now, let's create a brand-new database:

$ createdb testdb

No news is good news; if this is successful there will be no feedback. You'll see a message only if something went wrong. Now let's destroy our new database:

$ dropdb testdb

Again, silence equals success.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Data CenterWhite Papers & Webcasts

Webcast On Demand

Cloud Knowledge Vault

Sponsor: HP and Intel®

See more White Papers | Webcasts

Answers - Powered by ITworld

Ask a Question
randomness