Create database from command line - postgresql

I am trying to create a database from command line.
My OS is centos and postgres version is 10.9.
sudo -u postgres psql createdb test
Password for user test:
Why is it prompting me for the password?

Change the user to postgres :
su - postgres
Create User for Postgres (in the shell and NOT with psql)
$ createuser testuser
Create Database (same)
$ 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

PostgreSQL Create Database - Steps to create database in Postgres.
Login to server using postgres user.su - postgres
Connect to postgresql database.
bash-4.1$ psql
psql (12.1)
Type "help" for help.
postgres=#
Execute below command to create database.
CREATE DATABASE database_name;
Check for detailed information below:
https://orahow.com/postgresql-create-database/

PGPORT=5432
PGHOST="my.database.domain.com"
PGUSER="postgres"
PGDB="mydb"
createdb -h $PGHOST -p $PGPORT -U $PGUSER $PGDB

With a single command line:
su -c "createuser dbuser;createdb -h localhost -p 5432 -E UTF8 -O dbuser dbname;" - postgres

It is pretty simple but sometimes I find the answers tricky.
[For windows users]
Open Windows cmd
psql -U <username>
Once connected to psql, enter the following command to create a new database: CREATE DATABASE <database_name>;
To verify that the database has been created, you can run the \l command to list all available databases. Your new database should be listed in the output.------[Additional]------
You can now connect to the new database using the \c command followed by the database name, like this: \c <database_name>
You can now run SQL commands on the new database to create tables, insert data, and so on.
Note: Make sure to replace <username>, <database_name> with your actual Postgres username and database name.

If you are using pgAdmin:
In query editor you can try like this :
CREATE DATABASE <databasename>
WITH
OWNER = <dbowner>
ENCODING = <encoding>
CONNECTION LIMIT = <numberofsimulaneousconnections>;
an example snippet :
CREATE DATABASE twitterdb
WITH
OWNER = postgres
ENCODING = 'UTF8'
CONNECTION LIMIT = -1;

Related

Postgres ERROR: permission denied for table

I have a dump file generated by using pg_dump from a remote database. I am using the file to restore and populate a database using psql.
I did not have the user and database name. I peeked into the sql file and figured out the user and database.
Here are my steps:
1. psql -h <> -u <> -d ( I used the superuser here).
2. created the user as the application needs to access using this user only.
3. changed the owner of the database to the user created in step 2 using Pgadmin.
I am unable to access any tables and getting the error -- ERROR: permission denied for table
Please Help !
Thanks
It is better if you use dump and restore instead of psql for restoring the database.
e.g
e.g suppose you have database dbname_source in remoteDB and dbname_target in localDB then you can use the below commands for dump and restore
./pg_dump -U postgres -p 5432 -h <remote_IP> -Ft dbname_source > dump_of_source.sql.tar
./pg_restore -U postgres -h localhost -p 5432 -d dbname_target dump_of_source.sql.tar

What does psql -d postgres do?

What is the -d for and why do you need to type postgres when the psql already stands for postgres?
psql -d postgres
psql -d postgres
here -d stands for database name and hence postgres is your database name.
You can open the terminal by only typing psql, Above statement will do two things for you
Open psql terminal
Connect you to postgres db
For more psql options you can refer https://www.postgresql.org/docs/current/static/app-psql.html

PostgreSQL error Fatal: role “username” does not exist

I'm setting up my PostgreSQL 9.1 in windows.
I can't do anything with PostgreSQL: can't createdb, can't createuser; all operations return the error message
Fatal: role root does not exist
root is my account name, which I created while installing Postgresql
But I am able to connect using:
username : postgres
How can I connect to postgres using role root?
There is a solution mentioned for linux platforms using su command here but not able to figure out solution for windows7
Thanks in Advance
If you want to login to Postgres using the username root you need to first create such a user.
You first need to login as the Postgres super user. This is typically postgres (and is specified during installation):
psql -U postgres <user-name>
Then you can create roles and databases:
psql (9.4.0)
Type "help" for help.
postgres=# create user root with password 'verysecret';
CREATE ROLE
postgres=# \q
c:\
c:\>psql -U root postgres
psql (9.4.0)
Type "help" for help.
postgres=>
Logged in as the superuser you can also grant the root user the necessary privileges.
All parameters for psql are documented in the manual.
Creating users and databases is also documented in the manual:
connecting to the database
create user
create database
In some cases, when you install postgres the initial DB is not created.
You need to execute initdb.
Same issue appeared while restoring DB/table on postgres docker container .
. When you connect to Postgres DB(psql shell) from inside the docker container, the default user would be a "root" (unless you specify psql -U "some-user-name")
[Manjunath-MacBook-Air:$ sudo docker exec -it a2ff6075344e bash
bash-5.0# psql -U postgres postgres < testdb_pg_dump
So, the issue gets resolved, by logging to psql shell with appropriate username
Here , -U postgres specifies that user connecting to DB is "postgres"

How to run postgresql command from GCE shell?

Installing PostgreSQL on GCE requires root password to run sudo -u postgresql. This prompts for a password, which I was never given.
How do I get this pass, or any way to run postgresql commands from the shell in a different way?
Your system user postgresql doesn't have a password (I state with no proof... but I think you'll find this to be true.)
Normally you should use commands like these:
# Test that YOU can use psql (as postgres) to run a query:
psql -U postgres -c 'select * from pg_catalog.pg_user;'
# Test an interactive session:
psql -U postgres my_database
my_database=# select 42 as the_answer;
# Create a new database
psql -U postgres my_database
my_database=# create database mydb;
Alternatively, it's probably possible to login like this (it usually is):
sudo su postgres
And you could probably use this to run createdb. But running psql as yourself is probably better.
If you want to run commands against your postgresql server you should not need to use sudo, just use this syntax to enter the Postgresql Interactive Shell:
psql -U username database_name
OR
psql -U username hostname database_name
Replacing username with your postgresql usename, hostname (if not running on the same server) with the servers host name and database_name with the name of your database. For example:
psql -U postgressql customers
Normally sudo requires your user account password. So assuming that the account you are running the command from is listed in the sudoers file, the password it is prompting you for should be your own. Have you tried that, as opposed to the root or the postgresql password, which you don't appear to have (or might not even be set).

Postgres - How to use psql to create a database with the -c command and password authentication?

If I run the following:
psql -h localhost -U admin -c "CREATE DATABASE sales OWNER admin;"
It returns:
psql FATAL: database "admin" does not exist
I need to use password authentication and have setup .pgpass as follows:
localhost:*:*:admin:admin
Any ideas?
By default, when you connect as a user, the database connected to is the DB of the same name as the user.
If you want to connect to a different DB, you must specify it in the psql command line. Since in this case you're trying to create the DB, you can't connect to it yet, you must connect to another DB (like template1 or postgres) that already exists.
E.g.
psql -h localhost -U admin template1 -c "CREATE DATABASE sales OWNER admin;"
^^^^^^^^^