The first test to see whether you can access the database server is to try to create a database. A running PostgreSQL server can manage many databases. Typically, a separate database is used for each project or for each user.
Your site administrator might have already created a database for your use. He should have told you what the name of your database is. In this case you can omit this step and skip ahead to the next section.
To create a new database, in this example named mydb, you use the following command:
$ createdb mydb |
CREATE DATABASE |
Another response could be similar to this:
psql: could not connect to server: Connection refused Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"? createdb: database creation failed |
If you do not have the privileges required to create a database, you will see the following:
ERROR: CREATE DATABASE: permission denied createdb: database creation failed |
You can also create databases with other names. PostgreSQL allows you to create any number of databases at a given site. Database names must have an alphabetic first character and are limited to 31 characters in length. A convenient choice is to create a database with the same name as your current user name. Many tools assume that database name as the default, so it can save you some typing. To create that database, simply type:
$ createdb |
You can remove a database if you do not want to use it anymore. For example, if you are the owner (creator) of the database mydb, you can destroy it using the following command:
$ dropdb mydb |
[1] | As an explanation for why this works: PostgreSQL user names are separate from operating system user accounts. If you connect to a database, you can choose what PostgreSQL user name to connect as; if you don't, it will default to the same name as your current operating system account. As it happens, there will always be a PostgreSQL user account that has the same name as the operating system user that started the server, and it also happens that that user always has permission to create databases. Instead of logging in as that user you can also specify the -U option everywhere to select a PostgreSQL user name to connect as. |