Introduction
This is an article where the main focus is just to be able to show a certain process. That certain process is involving PostgreSQL database server. In other words, this article has a connection with several previous articles exist regarding PostgreSQL content material. In this article, the focus is just to show how to create a schema in a database exist in PostgreSQL database server. Normally, in a database exist in a PostgreSQL database server, there is a default schema exist which is ‘public’. So, it is possible to be able to create another schema in PostgreSQL database server beside ‘public’.
How to Create Schema in PostgreSQL Database Server
-
So, as with the other step involving PostgreSQL database server, just make sure that PostgreSQL database server is running. If there is no running PostgreSQL database server available, just start it. If there is no PostgreSQL database server exist, just install and run it.
-
After that, just try to access the PostgreSQL command console. It is necessary in order to execute the command for creating the schema. Just look at it in the following appearance :
Microsoft Windows [Version 10.0.22000.1219] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal>psql -Upostgres Password for user postgres: psql (14.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. postgres=#
-
Following after, just connect to a specific database. It is the database where the it will be target for creating a new schema in this context. As an example, the database is a database with the name of ‘mydb’. Below is the execution it in the PostgreSQL command console :
postgres=# \c mydb You are now connected to database "mydb" as user "postgres". mydb=#
-
After that, list all of the available schema in the database by typing the following command in order to show the different between the condition before creating a new schema and also after creating it :
mydb=# \dn * List of schemas Name | Owner --------------------+---------- asset | postgres information_schema | postgres report | postgres pg_catalog | postgres pg_toast | postgres product | postgres public | postgres test | postgres (8 rows) mydb=#
-
Suppose, there is a requirement in order to drop the schema, for an example the schema with the name of ‘test’, just execute the command with pattern as follows :
mydb=# create schema if not exists test; CREATE SCHEMA mydb=#
-
Check again whether the schema has already been perfectly deleted by executing the command once more :
mydb=# \dn * List of schemas Name | Owner --------------------+---------- asset | postgres information_schema | postgres report | postgres pg_catalog | postgres pg_toast | postgres product | postgres public | postgres test | postgres (8 rows) mydb=#