Create new database postgresql
Here are the steps to create a new database in PostgreSQL:
Using the psql
command-line tool:
-
Open a terminal or command prompt.
-
Connect to the PostgreSQL server using the
psql
command:psql -U your_username
Replace
your_username
with your PostgreSQL username. -
Create a new database using the following command:
CREATE DATABASE mydatabase;
Replace
mydatabase
with the name of your new database. -
To verify that the database was created, use the
\l
command:\l
This will list all the databases on your PostgreSQL server, including the new one you just created.
Using the pgAdmin
GUI tool:
- Open
pgAdmin
, a GUI tool for managing PostgreSQL databases. - Connect to your PostgreSQL server by clicking on "File" > "Connect to Database" and entering your server details.
- In the "Object Browser" panel, right-click on "Databases" and select "New" > "Database".
- Enter a name for your new database in the "Database name" field, and click "Save".
- The new database will be created and listed in the "Object Browser" panel.
Using a SQL script:
- Create a new file with a
.sql
extension (e.g.,create_database.sql
) and add the following command:CREATE DATABASE mydatabase;
- Save the file.
- Open a terminal or command prompt and navigate to the directory where you saved the file.
- Run the command:
psql -U your_username -f create_database.sql
Replace
your_username
with your PostgreSQL username.
This will execute the SQL script and create a new database named mydatabase
.
That's it! Your new database is now created and ready to use.