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

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;"
^^^^^^^^^

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

Cannot access postgres PSQL

I have just installed postgres (mac os), however, when I go to access psql it tries to find the database not the user.
I know the username is benbagley (because that's my system name)
I have tried.
➜ ~ psql
psql: FATAL: database "benbagley" does not exist
and
➜ ~ psql -U benbagley
psql: FATAL: database "benbagley" does not exist
As stated in the manual psql assumes that database name is same as the username when the database is not provided explicitly and tries to connect with that database, which does not exist in your case.
Try connecting with default database which is postgres.
psql -U username -d postgres

psql login fails database not found

using postgresql 9.4 on ubuntu 15.04.
logged in as postrges
created following roles: fooadmin, fooread, fooupdate with appropriate privileges for the role.
created following user: fooadminuser
granted role to user: grant fooadmin to fooadminuser;
log out of psql
attempt to login to psql using the following:
psql -U fooadminuser -h localhost -W
get prompted for password/enter password
receive following error message:
psql: FATAL: database "fooadminuser" does not exist
I thought I was doing this correctly by creating the roles, then creating the login users, then login with the user that has the createdb role and create the database.
Is there a way to override during login the attempt to access a non-existent database?
You have to connect to an existing database. If You don't provide it, the default is the same as the user. After installation there should be one database named 'postgres'. Try this command:
psql -U fooadminuser -h localhost -d postgres -W

Create database from command line

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;

Setting up postgres on VPS for production

So, I have a Pyramid app with a postgres database on my local machine. I did a pg_dump to get a dump of the data in my database, called pg_dump_2014-04-22. I then git pushed this file, and did a git pull in the VPS to get the file.
Now, I have already installed postgres on my VPS. When I sudo -u postgres psql on my VPS, I can connect to it but there are no relations (naturally).
Both my username and database name are postgres.
So, I tried psql postgres < pg_dump_2014-04-22, but this gives the error psql: FATAL: role "root" does not exist.
I also tried pg_restore -h localhost -U postgres -d postgres pg_dump_2014-04-22, and that prompts me for my password, but then throws the error pg_restore: [archiver(db)] connection to database "postgres" falied: FATAL: password authentication failed for user postgres"
What am I missing here?
You first have to create a user and the database where you want to import your dump
su postgres
createuser root
createdb yourdb
Then import the dump with
psql -d yourdb -f pg_dump_2014-04-22