Create or Drop the database in PostgreSQL

Create or Drop the Database in PostgreSQL

Create the database in PostgreSQL:

Syntax:

CREATE DATABASE dbname;

Example:

postgres=# create database testdb;
CREATE DATABASE

Check the database present on the PostgreSQL server:

postgres=# \l
                                                        List of databases
   Name    |  Owner   | Encoding |      Collate       |       Ctype        | ICU Locale | Locale Provider |   Access privileges
-----------+----------+----------+--------------------+--------------------+------------+-----------------+-----------------------
 postgres  | postgres | UTF8     | English_India.1252 | English_India.1252 |            | libc            |
 template0 | postgres | UTF8     | English_India.1252 | English_India.1252 |            | libc            | =c/postgres          +
           |          |          |                    |                    |            |                 | postgres=CTc/postgres
 template1 | postgres | UTF8     | English_India.1252 | English_India.1252 |            | libc            | =c/postgres          +
           |          |          |                    |                    |            |                 | postgres=CTc/postgres
 testdb    | postgres | UTF8     | English_India.1252 | English_India.1252 |            | libc            |
(4 rows)

Connect to the New Created database:

C:\Program Files\PostgreSQL\15\bin>psql -U postgres -d testdb
Password for user postgres:
psql (15.2)
WARNING: Console code page (437) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.
testdb=# create table test (id int);
CREATE TABLE

Drop the database in PostgreSQL:

postgres=# drop database testdb;
DROP DATABASE
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.