Postgresql creating database - postgresql

Well I installed the latest postgreql database on my Windows 7.
Now I'm trying to create a database via the psql.exe command line
When I open it, it says
psql: FATAL: database "Jansu" does not exist
So I read somewhere, that when no database is specified, it tried to find database with my username or something.
Anyways..how do i create a new database, when I can't access the commandline.

Read psql syntax. You can specify database, user and other parameters. If it's a new installation, there should be a default database 'postgres', you can connect to that one.
psql -U postgres postgres
(In Unix environments you might need to add -h localhost in order to force a TCP connection, otherwise it'd try to use Unix-domain sockets, which might not work for other than the postgres user. )
You can create databases from there, or from the command line with createdb

Related

List all databases in PostgreSQL when there isn't a db with same name of the current user's

First of all, I am new to PostgreSQL.
So am I right thinking that one cannot run most of the psql util commands nor non-db-specified sql commands if there isn't a db with same name of the current user's?
That is saying e.g., if I run psql "show databases;" as user postgres while there isn't a db called "postgres", I won't be able to run the command.
Question is that in this case, one cannot find the list of the dbs before knowing any of db exits, is that how it works?
You have to connect to a database. By default, the databases "template1" and "postgres" will exist and will accept connections.
If your PostgreSQL admin has changed things in such a way that you can't connect to either of those databases, you'll have to do one of two things.
Ask the PostgreSQL admin what database you're supposed to connect to.
Create a database, then connect to it. There's more than one way to do this.
If you have CREATEDB privileges, you can create a database on the psql command line. For example, I have CREATEDB privileges here, so I can do this, which creates the database "mike" and exits.
$ psql -h localhost -p 5435 -U mike -c "create database mike"
Now I can connect to "mike" by either taking advantage of the default database name, or by specifying it.
$ psql -h localhost -p 5435 -U mike
$ psql -h localhost -p 5435 -U mike mike
You can. If you connect (with proper user, usually postgres) to the postgres database there are several tables on the pg_catalog (PostgreSQL) among those is pg_database table a simple select * from pg_database will show all databases.
Here is an image showing that on pgAdmin III Tool
There is no way of doing exactly what you want without, at least, knowing the database catalog. The postgres database is default and will exist in all installed instances (unless someone had droped it). All RDBMs is the same they all have the catalog (also named information_schema, or other names depending on vendor) which holds all information about the databases, tables, constraints, etc.

.pgpass with AWS RDS

I need to run multiple commands on an AWS Postgres RDS instance I have. I don't want to enter the password each time. I'm trying to use the .pgpass file but I'm running into errors. The first time I ran into an error which said 'role "ubuntu username" does not exist'. I logged in as the postgres user and created that username. After this, the error I get said database does not exist. I have a feeling these errors have nothing to do with trying to connect to the AWS RDS instance.
psql --host=<awshost> --port=5432 --username=<awsrdsusername> --password --dbname=<dbname asks for a password and then logs me in after I enter it. Now I put a file in /home/<ubuntuusername> called .pgpass which has <awshost>:5432:<dbname>:<awsrdsusername>:<password>. Permissions for this file are set to 0600. Now when I run psql from the terminal and that produces the error - psql: FATAL: role "<ubuntuusername>" does not exist. These steps are as outlined on this page.
Can someone help me with the steps to get a pgpass file to connect to an AWS RDS instance?
.pgpass doesn't provide connection information. You seem to expect that after you create a .pgpass file, you can run psql without arguments and it'll know where to connect. That is not the case.
The hostname, port, etc you put in .pgpass are there so that PostgreSQL knows which line to look at when matching the connection info to find its password.
If you had to run:
psql -h something
to connect without a .pgpass file, you still have to run the same thing to connect with a .pgpass file.
If you run psql without arguments it'll connect to the local PostgreSQL (if any), using the current unix username as the postgresql username and the database to connect to. That's why you get the error you do.
If you want to change the default connection, you can use environment variables like PGHOST, PGPORT, etc, and/or a .pgservice.conf file.
See the manual to learn more.

PostgreSQL 9.4, 64-bit windows: Unable to create user from commandline

I'm trying to create a user from command line using PostgreSQL version 9.4 on a 64 bit machine with windows.
I'm using the below command to create a new user:
createuser -d temba
On executing the above command, it prompts me for a password.
I enter the password (qwerty) which i used while installing PostgreSQL. on doing so, i get the below error:
psql: FATAL: password authentication failed for user "my-windows-user-name"
Next, i tried giving my login password for windows, i get the same error as above.
Can anyone guide me through the procedure for creating a new user from command line (only, I'm not allowed to use PgAdmin to create user).
I have checked previous posts with similar errors. But the problem there is, either they are not using windows or they are using a really old version of PostgreSql.
Any information on how to proceed about with this shall be really helpful.
Thanks in advance.
All Postgres command line tools try to connect to the database using the current operating system user if no database user is specified.
So you need to specify the user name of the Postgres superuser that is used to connect to the Postgres server:
createuser -U postgres -d temba
This becomes more evident if you use psql instead. You specify the Postgres user account and the target database when you start it:
psql -U postgres -d temba
Then at the prompt you can run create user ....
temba=# create user foobar ... ;
Which is what the command line tool createuser is doing in the background.

psql: FATAL: database "Morne Nel" does not exist

I need to do a report in JasperReports.... besides the point here.
Ive been given a existing Postgresql DB to use.
I have installed PostgreSQL and all went great. (except the package update at the end. Cant get past the proxy server)
Ive added the C:\Program Files\PostgreSQL\9.0\bin path the the system path as-well.
When I go to cmd and type psql it propts for a password....
I enter password, because thats the only password I added during instalation.
Then is comes up with psql: FATAL: database "Morne Nel" does not exist
step by step hoe do I import this DB into postgres? PLEASE?
When using psql you have to pass database name, otherwise postgres will try to connect to database matching your user name (that is why it tries to connect to "Morne Nel").
You can use createdb console command to create database first. Here is documentation on that command: http://www.postgresql.org/docs/9.3/static/sql-createdatabase.html
But also you can use pg_restore command right away with --create option passed to create and restore database from dump at once (documentation here: http://www.postgresql.org/docs/9.3/static/app-pgrestore.html).
Try to execute those commands (it is possible that you will have to specify your postgres user and password) and let us know what happened. I cannot write more at the moment as I am not sure what dump format do you have so it would be good to provide that information if you won't manage to use pg_restore successfully.

Specifying the default database for psql to connect to?

While using psql, I want to change the initial database connection.
I had a database named "test" as the initial connection.
When running psql from the command line my prompt would be test=#
After deleting the "test" database, and running psql in command line, I get the following error:
psql: FATAL: database "test" does not exist
I understand what this means, but how do I go about setting the "postgres" database as the default?
Instead of typing psql postgres each time.
You can configure the default behavior of psql -- and in fact any program using the libpq client library -- through environment variables.
In your ~/.bashrc or similar:
export PGDATABASE=postgres
The PostgreSQL documentation contains a complete list.