Tuesday, August 31, 2021

PostgreSQL - Create Database

In this tutorial you'll learn how to create database in PostgreSQL using both-
  1. SQL shell (command line)
  2. pgAdmin4 (Graphical interface)

Creating DB using SQL shell

Command that you can use to create a new DB is-

CREATE DATABASE dbname;
  1. Open SQL shell, provide the user name and password to connect to server.
  2. Type command CREATE DATABASE testdb; to create a database named testdb.
create database Postgres

Please refer https://www.postgresql.org/docs/13/sql-createdatabase.html to see all the options for creating database in PostgreSQL.

You can use the \l command to see the list of DBs in PostgreSQL.

You can switch connections to a new database using \c command.

postgres=# \c testdb
You are now connected to database "testdb" as user "postgres".

Creating DB using pgAdmin

In the left pane right click on Databases-Create-Database to open "Create - Database" window.

create database PostgreSQL pgAdmin

In the "Create - Database" window enter name of the DB and comment (optional). Click save to create the DB

Once the DB is created you will see the message for the same and the created DB in the left pane under the Databases.

You can verify the SQL that is executed for creating the DB by selecting the created DB in the left pane and selecting the SQL option on the right side.

That's all for this topic PostgreSQL - Create Database. If you have any doubt or any suggestions to make please drop a comment. Thanks!


You may also like-

  1. How to Install PostgreSQL on Windows
  2. Types of JDBC Drivers
  3. Java JDBC Steps to Connect to DB
  4. Transaction Management in Java-JDBC
  5. Batch Processing in Java JDBC - Insert, Update Queries as a Batch
  6. Try-With-Resources in Java With Examples
  7. Spring Boot REST API CRUD Example With Spring Data JPA
  8. Spring NamedParameterJdbcTemplate Insert, Update And Delete Example

No comments:

Post a Comment