EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL Database

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL Database

PostgreSQL Database

Introduction to PostgreSQL Database

PostgreSQL database is an open-source database system also an object-relational database system it is accessed by using psql command, the current latest version of PostgreSQL is 12, we create a database using create database statement and drop the database using drop database statement, in PostgreSQL we can query to the database using database SQL prompt and OS command prompt, to query using database SQL prompt we need to login into the database by using username, password, and connecting database name, If we query to the database using OS command prompt also need to provide username and password.

How to Create a Database in PostgreSQL?

We can create a database by using the create database statement. We can create a database in PostgreSQL by using two methods:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Create a database statement

  • The database is created using the SQL command to create database statements. Please find below syntax and example for creating a database statement.

Syntax

Create database database_name;

Example

Postgres=# Create database db_testing;

Output:

PostgreSQL Database output 1

  • In the above syntax, the name shows the database name we have used to create a new database in PostgreSQL. We can assign owner database user privileges to other users or change the database user’s owner later.
  • After installing PostgreSQL first database is created by using initdb command.

Below are the default databases that were created at the time of installation.

  1. Postgres
  2. Template0
  3. Template1
  • Postgres database is the administrative database of all the users in PostgreSQL. It is the default connection database of admin users.
  • Template0 and template1 are the Meta database of other databases. At the time of creating a new database, these databases are used.

Create DB statement

  • We can create a database by using the shell prompt, using the command as Createdb statement.
  • Please find below syntax and example for createdb statement.
  • Createdb statement connects to the database, and issues create database command by using Createdb on shell prompt.
  • If we have not given any arguments at the time of database creation, it will create the database name the same as the username.

Syntax 

Createdb The above syntax shows createdb as the command to create a database on the shell.

createdb database_name Database name refers to give any name to the newly created database.

createdb –U username database_name Username refers to the database user name.

Popular Course in this category
Sale
PostgreSQL Course (2 Courses, 1 Project)2 Online Courses | 1 Hands-on Project | 7+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (6,195 ratings)
Course Price

View Course

Related Courses

Example

createdb
createdb test_db
createdb –U postgres db_test
\l+ test_db;
\l+ db_test;

Output:

PostgreSQL Database output 2

  • We can also set the database owner in PostgreSQL. The database owner has all the privileges and permissions to execute database related queries.
  • We can query or select the PostgreSQL database using two methods.

Please find below the methods to query the database:

  • Database SQL prompt
  • OS command prompt
1. Database SQL prompt
  • In the Database SQL prompt, first, we need to login into the PostgreSQL database.
  • We can login into the database by providing a username, password, hostname and database name.
  • Please find below the example to login to the database by providing a username and password.

Example

psql –U postgres –d postgres

Output:

PostgreSQL Database output 3

2. OS command prompt
  • We can select the PostgreSQL database using the OS command prompt.
  • We can select the OS prompt database by supplying the same parameter that we have using in the database SQL prompt.
  • We have to use parameters like username, password, port, hostname, etc.
  • Please find below the example for the OS command prompt are as follows.

Example

psql -U postgres -d testing -c "select * from employee";
psql -U postgres -d testing -c "select version()";

Output:

output 4

How to select a particular database from multiple databases?

  • We can select a particular database from multiple databases by using the \l command.
  • We can use the below command to select a particular database from multiple databases in PostgreSQL.
  • Mostly we have using \l db_name command to select a particular database in PostgreSQL.
  • Please find below syntax and example for the same.

Syntax

      Please find below syntax to select a particular database from multiple databases in PostgreSQL.

\l database_name The database name refers to the specific name of the newly created database in PostgreSQL.

\l+ database_name  If we need a descriptive output of the database, then we have used \l+ and database name to display descriptive output.

Example

Please find below the example to select a particular database from multiple databases.

\l db_testing;
\l+ db_testing;

Output:

output 5

  • In the above example, we have selected the db_testing database from multiple databases like db_test, Postgres, and test_db, etc.
  • Also, we have checked the descriptive output of the db_testing database by using the \l+ db_testing command.
  • This is an essential feature and commands to select a single database from multiple databases.

How to drop a database?

  • We can drop the database using the drop database command in PostgreSQL. We can drop the database using login to the database and using the OS command prompt in PostgreSQL.
  • If we need a database no longer, then we delete a specific database by issuing a drop database command in PostgreSQL.
  • Drop database statement deletes database entry, catalogs, and data directory permanently.
  • Only database owners of the database can execute the drop database command; other users have not permission to execute the drop database command.
  • Also, the admin user has permission to execute the drop statement command.
  • In this dropdb program, utility available to drop the database. This utility allows you to remove the database. This utility executes the drop statement command behind the scene.

Please find below syntax and example to drop the database in PostgreSQL:

Syntax

drop database database_name

Example

postgres=# drop database db_testing;

Output:

output 6

Conclusion

We can create a database using create database statement; also, we have to define the database owner at the time of database creation. We can drop the database by using the drop database statement; only the owner and database admin has privileges to drop databases in PostgreSQL.

Recommended Articles

This is a guide to PostgreSQL Database. Here we discuss how to create a database, drop the database and select data from multiple databases along with the respective examples. You may also have a look at the following articles to learn more –

  1. What is PostgreSQL?
  2. PostgreSQL Operators
  3. PostgreSQL String Functions
  4. PostgreSQL Interview Questions
  5. Sequence in PostgreSQL | Examples

PostgreSQL Course (2 Courses, 1 Project)

2 Online Courses

1 Hands-on Project

7+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - PostgreSQL Course (2 Courses, 1 Project) Learn More