SQL: create command. create is a DDL SQL command used to create a table or a database in relational database management system.
MySQL Command Line Client We can create a new database in MySQL by using the CREATE DATABASE statement with the below syntax: CREATE DATABASE [IF NOT EXISTS] database_name. [CHARACTER SET charset_name] [COLLATE collation_name];
Change the user to postgres :
su - postgres
Create User for Postgres
$ createuser testuser
Create Database
$ createdb testdb
Acces the postgres Shell
psql ( enter the password for postgressql)
Provide the privileges to the postgres user
$ alter user testuser with encrypted password 'qwerty';
$ grant all privileges on database testdb to testuser;
Try:
sudo -u postgres psql -c 'create database test;'
createdb
is a command line utility which you can run from bash and not from psql. To create a database from psql, use the create database statement like so:
create database [databasename];
Note: be sure to always end your SQL statements with ;
As some of the answers point out, createdb
is a command line utility that could be used to create database.
Assuming you have a user named dbuser
, the following command could be used to create a database and provide access to dbuser
:
createdb -h localhost -p 5432 -U dbuser testdb
Replace localhost
with your correct DB host name, 5432
with correct DB port, and testdb
with the database name you want to create.
Now psql
could be used to connect to this newly created database:
psql -h localhost -p 5432 -U dbuser -d testdb
Tested with createdb
and psql
versions 9.4.15
.
As the default configuration of Postgres, a user called postgres is made and the user postgres has full super admin access to entire PostgreSQL instance running on your OS.
sudo -u postgres psql
The above command gets you the psql command line interface in admin mode.
Creating user
sudo -u postgres createuser <username>
Creating Database
sudo -u postgres createdb <dbname>
NOTE: < > are not to be used while writing command, they are used just to signify the variables
PGPORT=5432
PGHOST="my.database.domain.com"
PGUSER="postgres"
PGDB="mydb"
createdb -h $PGHOST -p $PGPORT -U $PGUSER $PGDB
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With