20 March 2011

Create and Delete a PostgreSQL Database from PSQL CLI

Easily create or drop a database via the psql CLI using the below syntax.
Create a PostgreSQL Database: Just modify the database_name to the name you prefer for your new database.
create database database_name

Delete a PostgreSQL Database: Again just modify the database_name to the name of the database you would like to remove from Postgres.
drop database database_name
Below is output from a shell where a connection is made to Postgres  with psql, a database is added, and then the same database is deleted.  In the example below the database that is added and removed is called  “newdb”.
[root@server ~]# psql -U postgres
Welcome to psql 8.3.5, the PostgreSQL int terminal.

Type: copyright for distribution terms
h for help with SQL commands
? for help with psql commands
g or terminate with semicolon to execute query
q to quit

postgres=# create database newdb;
CREATE DATABASE
postgres=# drop database newdb;
DROP DATABASE
postgres=# q
[root@server ~]#
biOos

No comments: