Using the database in PostgreSQL
This article demonstrates connecting to a database, creating a new database, and creating a table in PostgreSQL.
Available databases in PostgreSQL
You can run the following command after opening the Postgres command line to view all available databases that exist and are connected.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+----------------------------+----------------------------+-----------------------
postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
Here, you can see 3 行
that this means we have three databases in Postgres. You need to understand that database does not mean table.
There can be multiple tables in a database. In addition, the table may be related to other tables.
Let's see how to connect to the database.
When you first install Postgres, you will find these three databases created for you by default. If you do not connect to any database, by default all the tables you create later will go to Postgres
the database named .
Connecting to a database in PostgreSQL
You need to open psql
a shell or open from the terminal psql
to connect to the database. Then using the credentials, log in to Postgres; after that, use the following command.
postgres=# \c postgres
You are now connected to database "postgres" as user "postgres".
So, the terminal says that you are now connected to a Postgres
database called . Let's see what tables we have in this database.
To see the list of tables available in the database, we need to write the command \dt <database_name>
. For example:
postgres=# \c postgres
You are now connected to database "postgres" as user "postgres".
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | accounts | table | postgres
(1 row)
Create a new database in PostgreSQL
Suppose you need a database of your own and you will manage some tables there. The basic syntax to create a database is CREATE DATABASE <database_name>
.
Once created, let's create a titan
database called and view the list of available databases.
postgres=# CREATE DATABASE TITAN;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+----------------------------+----------------------------+-----------------------
postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
titan | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
(4 rows)
postgres=# \c titan
You are now connected to database "titan" as user "postgres".
In addition, there is another way to create a database. The format is createdb [options...] [database_name [description of database]]
.
Here is a list of the options available in [PostgreSQL](https://postgrespro.com/docs/postgresql/9.6/app-createdb).
Options | illustrate |
---|---|
-D | Default tablespace for the database |
-h | hostname of the machine where the server is sunny |
-e |
Echo the commands that createdb generates and sends to the server |
-E | Specifies the character encoding scheme to be used in this database |
Here is the complete list of options from the official PostgreSQL documentation.
Create a table in the PostgreSQL connection database
While we are connected to titan
a database named , let's see if any tables exist.
postgres=# \c titan
You are now connected to database "titan" as user "postgres".
titan=# \dt
Did not find any relations.
As you can see, there are no tables in the database. Also, if you notice, there are some minor changes here.
When we are connected to titan
a database, the line titan=#
starts with , which means that the console titan
is running on the database.
Let us create a table here as shown below.
CREATE TABLE Colossal (
titan_id serial PRIMARY KEY,
titan_name VARCHAR ( 50 ) NOT NULL,
strength_level INT NOT NULL
);
CREATE TABLE
titan=# \dt
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | colossal | table | postgres
(1 row)
titan=#
Now, you can perform operations on the table CRUD
. We can see colossal
that the table is now available in titan
the database.
For more information on database creation and setup, follow the official documentation .
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Terminate the PostgreSQL connection
Publish Date:2025/04/11 Views:199 Category:PostgreSQL
-
In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep
Single query to rename and change column type in PostgreSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
This article describes how to rename a column and change its type in PostgreSQL using only a single query. Renaming and changing column types in MySQL In MySQL , if you want to change the column type and rename it, you can use a simple stat
Joining columns using Select in PostgreSQL
Publish Date:2025/04/11 Views:176 Category:PostgreSQL
-
MySQL PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitors . Today we will learn how to use SELECT the operator to join the columns of a table. Using operators to || joi
Using CASE in PostgreSQL
Publish Date:2025/04/11 Views:124 Category:PostgreSQL
-
This article shows how to use the statement in PostgreSQL CASE . CASE How to use the statement in PostgreSQL case Statements are similar to those in general-purpose programming languages if-else . But in SQL, if you want to write IF-ELSE ,
Using NOT IN with subqueries in PostgreSQL
Publish Date:2025/04/11 Views:93 Category:PostgreSQL
-
NOT IN The inverts the result of NOT simply using IN the operator. NOT IN The right side of the operator must have a subquery in which multiple columns are returned to check whether the expression matches the data. NOT IN Tends to return tr
Using variables in PostgreSQL
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article will demonstrate how we can declare and assign values to variables in PostgreSQL. In PostgreSQL, DECLARE variables are declared using Often you will need variables in your PL/SQL scripts. In DECLARE the section called , y
Connect to PostgreSQL using a password
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpass a file, PGPASSWORD an environment variable or a connection string. Connecting to PostgreSQL with a password using the comm
Deleting a database in PostgreSQL via PSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
There are two ways to access PostgreSQL objects and databases on your system. One is through an interface, such as a graphical interface like PGADMIN, and the other is the basic command line tool psql. Today, we will look at DROP DATABASE t
PostgreSQL replace string
Publish Date:2025/04/11 Views:91 Category:PostgreSQL
-
This article discusses how to use PostgreSQL replace() functions to replace strings. Use the PostgreSQL replace() function to replace strings PostgreSQL replace() functions have the following arguments, which are all text types: replace (st