Managing Databases with PostgreSQL
Read the documentation for more detailed information.
The simplest way to create a new database is to use the createdb command. If you are going to use PostgreSQL
frequently, it’s a good idea to add the PostgreSQL bin directory to your PATH.
$ createdb --helpcreatedb creates a PostgreSQL database.
Usage: createdb [OPTION]... [DBNAME] [DESCRIPTION]
Options: -D, --location=PATH alternative place to store the database -E, --encoding=ENCODING encoding for the database -O, --owner=OWNER database user to own the new database -T, --template=TEMPLATE template database to copy -e, --echo show the commands being sent to the server -q, --quiet don't write any messages --help show this help, then exit --version output version information, then exit
Connection options: -h, --host=HOSTNAME database server host or socket directory -p, --port=PORT database server port -U, --username=USERNAME user name to connect as -W, --password prompt for password
By default, a database with the same name as the current user is created.
Report bugs to <pgsql-bugs@postgresql.org>.
$ createdb -e testPassword:CREATE DATABASE test;CREATE DATABASEThe database named test has been created.
Listing all available Databases
Section titled “Listing all available Databases”Use the following psql -l command.
$ psql -lPassword: List of databases Name | Owner | Encoding-----------+----------+----------- template0 | postgres | SQL_ASCII template1 | postgres | SQL_ASCII test | jurn | SQL_ASCII(3 rows)The databases template0 and template1 are created when you initialise the cluster. The test database was created in the previous example.
Dropping or Removing a Database
Section titled “Dropping or Removing a Database”Use the dropdb command:
$ dropdb -e testPassword:DROP DATABASE test;DROP DATABASE