Problems accessing databases on remote postgresql sever - postgresql

I have a postgresql (9.1.4) server running on a remote machine (Ubuntu 12.04), and I'm having trouble accessing it on my local machine. Specifically, on my local machine, I can access the remote server using only the username "postgres" and the database name "postgres", even though I have other user names and databases. First some of the background information. While in the interactive environment invoked with "psql" on the remote machine, I created another user, called "mxtxdb", and set the password for that user. I also created a database called "mxtxdb". To demonstrate their existence, I logged onto the remote machine and ran:
sudo su postgres
Password:
postgres#myhost:~$ psql
psql (9.1.4)
Type "help" for help.
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
mxtxdb | | {}
postgres | Superuser, Create role, Create DB, Replication | {}
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
mxtxdb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres+
| | | | | mxtxdb=CTc/postgres
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
Furthermore, while still on the remote machine, I can access the database from the "mxtxdb" postgresql account:
postgres#myhost:~$ psql -h localhost -U mxtxdb -d mxtxdb
Password for user mxtxdb:
psql (9.1.4)
Type "help" for help.
mxtxdb=>
But when I try to do the same on the local machine, I get this:
psql -h <server's address> -U postgres -d "postgres" -p 5432
Password for user postgres:
psql (9.1.4)
Type "help" for help.
postgres=#
and
psql -h <server's address> -U "postgres" -d "mxtxdb" -p 5432
Password for user postgres:
psql: FATAL: database "mxtxdb" does not exist
and
psql -h <server's address> -U "mxtxdb" -d "mxtxdb" -p 5432
Password for user mxtxdb:
psql: FATAL: role "mxtxdb" does not exist
The last two lines of my pg_hba.conf file are:
host all mxtxdb 0.0.0.0/0 trust
host all postgres 0.0.0.0/0 trust
and the listen_addresses is set to '*' (and uncommented) in the postgresql.conf file.
Any idea why I cannot access other accounts or databases remotely, but I can when ssh'd into the remote machine?

First of all, thanks to Craig Richard for your suggestions, and in fact you were both correct. Here's what happened: I installed PostgreSQL using sudo apt-get install postgresql-9.1, and then I followed the instructions for configuring the server at http://www.postgresql.org/docs/9.1/interactive/runtime.html . Unfortunately, what I did not notice was that the apt-get install process creates a postgresql database cluster by default, so when I followed the instructions to create a new one, I then had two clusters, one at /var/lib/postgresql/9.1/main and one at /usr/local/pgsql/data, and I was interacting with different clusters when I logged in locally vs remotely. When I removed the second cluster, the issue was resolved and I was able to connect remotely.
My takeaways from this process: when installing postgresql with apt-get, and then following the instructions in the postgresql manual, be aware that the install process will create a postgres operating system user by default, and will also create a database cluster by default, so those steps in the manual do not need to be performed. If anyone experiences an error like:
"FATAL: role/database '<name>' does not exist"
when you are sure you've created that user or database, check to ensure that you're operating with the correct database cluster.
Thanks again!

Related

postgresql permissions denied [duplicate]

I'm using the PostgreSql app for mac (http://postgresapp.com/). I've used it in the past on other machines but it's giving me some trouble when installing on my macbook. I've installed the application and I ran:
psql -h localhost
It returns:
psql: FATAL: database "<user>" does not exist
It seems I can't even run the console to create the database that it's attempting to find. The same thing happens when I just run:
psql
or if I launch psql from the application drop down menu:
Machine stats:
OSX 10.8.4
psql (PostgreSQL) 9.2.4
Any help is appreciated.
I've also attempted to install PostgreSql via homebrew and I'm getting the same issue. I've also read the applications documentation page that states:
When Postgres.app first starts up, it creates the $USER database,
which is the default database for psql when none is specified. The
default user is $USER, with no password.
So it would seem the application is not creating $USER however I've installed->uninstalled-reinstalled several times now so it must be something with my machine.
I found the answer but I'm not sure exactly how it works as the user who answered on this thread -> Getting Postgresql Running In Mac: Database "postgres" does not exist didn't follow up. I used the following command to get psql to open:
psql -d template1
I'll leave this one unanswered until someone can provide an explanation for why this works.
It appears that your package manager failed to create the database named $user for you. The reason that
psql -d template1
works for you is that template1 is a database created by postgres itself, and is present on all installations.
You are apparently able to log in to template1, so you must have some rights assigned to you by the database. Try this at a shell prompt:
createdb
and then see if you can log in again with
psql -h localhost
This will simply create a database for your login user, which I think is what you are looking for. If createdb fails, then you don't have enough rights to make your own database, and you will have to figure out how to fix the homebrew package.
From the terminal, just Run the command on your command prompt window. (Not inside psql).
createdb <user>
And then try to run postgres again.
By default, postgres tries to connect to a database with the same name as your user. To prevent this default behaviour, just specify user and database:
psql -U Username DatabaseName
Login as default user: sudo -i -u postgres
Create new User: createuser --interactive
When prompted for role name, enter linux username, and select Yes to superuser question.
Still logged in as postgres user, create a database: createdb <username_from_step_3>
Confirm error(s) are gone by entering: psql at the command prompt.
Output should show psql (x.x.x) Type "help" for help.
Login using default template1 database:
#psql -d template1
#template1=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+---------+----------+-------------+-------------+---------------------
postgres | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca +
| | | | | gogasca=CTc/gogasca
template1 | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca +
| | | | | gogasca=CTc/gogasca
(3 rows)
Create a database with your userId:
template1=# CREATE DATABASE gogasca WITH OWNER gogasca ENCODING 'UTF8';
CREATE DATABASE
Quit and then login again
template1=# \q
gonzo:~ gogasca$ psql -h localhost
psql (9.4.0)
Type "help" for help.
gogasca=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+---------+----------+-------------+-------------+---------------------
gogasca | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca +
| | | | | gogasca=CTc/gogasca
template1 | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca +
| | | | | gogasca=CTc/gogasca
(4 rows)
I faced the same error when I trying to open postgresql on mac
psql: FATAL: database "user" does not exist
I found this simple command to solve it:
method1
$ createdb --owner=postgres --encoding=utf8 user
and type
psql
Method 2:
psql -d postgres
Post installation of postgres, in my case version is 12.2, I did run the below command createdb.
$ createdb `whoami`
$ psql
psql (12.2)
Type "help" for help.
macuser=#
Step 1:
psql -d template1
now you should be on psql terminal
Step 2:
CREATE DATABASE username;
make sure you use semicolon (;) after the database name;
optional: on psql terminal type \ls or \l to list all the databases;
Step 3:
psql -h localhost
now you should be connected;
Try using-
psql -d postgres
I was also facing the same issue when I ran psql
Had the same problem, a simple psql -d postgres did it (Type the command in the terminal)
This error can also occur if the environment variable PGDATABASE is set to the name of a database that does not exist.
On OSX, I saw the following error while trying to launch psql from the Postgress.app menu:
psql: FATAL: database "otherdb" does not exist
The solution to the error was to remove export PGDATABASE=otherdb from ~/.bash_profile:
Further, if PGUSER is set to something other than your username, the following error will occur:
psql: FATAL: role "note" does not exist
The solution is to remove export PGUSER=notme from ~/.bash_profile.
Not sure if it is already added in the answers, Anatolii Stepaniuk answer was very helpful which is the following.
psql -U Username postgres # when you have no databases yet
As the createdb documentation states:
The first database is always created by the initdb command when the data storage area is initialized... This database is called postgres.
So if certain OS/postgresql distributions do that differently, it is certainly not the default/standard (just verified that initdb on openSUSE 13.1 creates the DB "postgres", but not "<user>"). Long story short, psql -d postgres is expected to be used when using a user other than "postgres".
Obviously the accepted answer, running createdb to create a DB named like the user, works as well, but creates a superfluous DB.
Since this question is the first in search results, I'll put a different solution for a different problem here anyway, in order not to have a duplicate title.
The same error message can come up when running a query file in psql without specifying a database. Since there is no use statement in postgresql, we have to specify the database on the command line, for example:
psql -d db_name -f query_file.sql
First off, it's helpful to create a database named the same as your current use, to prevent the error when you just want to use the default database and create new tables without declaring the name of a db explicitly.
Replace "skynotify" with your username:
psql -d postgres -c "CREATE DATABASE skynotify ENCODING 'UTF-8';"
-d explicitly declares which database to use as the default for SQL statements that don't explicitly include a db name during this interactive session.
BASICS FOR GETTING A CLEAR PICTURE OF WHAT YOUR PostgresQL SERVER has in it.
You must connect to an existing database to use psql interactively. Fortunately, you can ask psql for a list of databases:
psql -l
.
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------------------------------+-----------+----------+-------------+-------------+-------------------
skynotify | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
myapp_dev | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
ruby-getting-started_development | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/skynotify +
| | | | | skynotify=CTc/skynotify
template1 | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/skynotify +
| | | | | skynotify=CTc/skynotify
(6 rows)
This does NOT start the interactive console, it just outputs a text based table to the terminal.
As another answers says, postgres is always created, so you should use it as your failsafe database when you just want to get the console started to work on other databases. If it isn't there, then list the databases and then use any one of them.
In a similar fashion, select tables from a database:
psql -d postgres -c "\dt;"
My "postgres" database has no tables, but any database that does will output a text based table to the terminal (standard out).
And for completeness, we can select all rows from a table too:
psql -d ruby-getting-started_development -c "SELECT * FROM widgets;"
.
id | name | description | stock | created_at | updated_at
----+------+-------------+-------+------------+------------
(0 rows)
Even if there are zero rows returned, you'll get the field names.
If your tables have more than a dozen rows, or you're not sure, it'll be more useful to start with a count of rows to understand how much data is in your database:
psql -d ruby-getting-started_development -c "SELECT count(*) FROM widgets;"
.
count
-------
0
(1 row)
And don't that that "1 row" confuse you, it just represents how many rows are returned by the query, but the 1 row contains the count you want, which is 0 in this example.
NOTE: a db created without an owner defined will be owned by the current user.
had the problem with using the JDBC driver, so one just has to add the database (maybe redundantly depending on the tool you may use) after the host name in the URL, e.g.
jdbc:postgres://<host(:port)>/<db-name>
further details are documented here: http://www.postgresql.org/docs/7.4/static/jdbc-use.html#JDBC-CONNECT
Connect to postgres via existing superuser.
Create a Database by the name of user you are connecting through to postgres.
create database username;
Now try to connect via username
This worked for me when solving this problem
i ran sudo -i -u postgress --> to gain access to my postgres database.
Then enter your password.
it would allow you to now enter psql
which would prompt you for other command
Cheers!
Had this problem when installing postgresql via homebrew.
Had to create the default "postgres" super user with:
createuser --interactive postgres answer y to for super user
createuser --interactive user answer y to for super user
you can set the database name you want to connect to in env variable PGDATABASE=database_name. If you dont set this psql default database name is as username. after setting this you don't have to createdb
Was running postgres in docker. In cli I was getting the error "root" role doesn't exist.
su - postgres
psql
solved the problem.
PostgreSQL has its own user on the system which is created when PostgreSQL is installed. The postgres user is able to log into PostgreSQL without using a password. No other user is able to log into PostgreSQL.
This means that before using PostgreSQL, you will need to switch to that user account with the command:
su - postgres
You will then be able to log into the PosgreSQL client with the command:
psql
You will not be able to access the database from the command line as any other user.
I still had the issue above after installing postgresql using homebrew - I resolved it by putting /usr/local/bin in my path before /usr/bin
This is a basic misunderstanding. Simply typing:
pgres
will result in this response:
pgres <db_name>
It will succeed without error if the user has the permissions to access the db.
One can go into the details of the exported environment variables but that's unnecessary .. this is too basic to fail for any other reason.

Problem with Docker Compose - FATAL: database "root" does not exist (PostgresSQL) [duplicate]

I'm using the PostgreSql app for mac (http://postgresapp.com/). I've used it in the past on other machines but it's giving me some trouble when installing on my macbook. I've installed the application and I ran:
psql -h localhost
It returns:
psql: FATAL: database "<user>" does not exist
It seems I can't even run the console to create the database that it's attempting to find. The same thing happens when I just run:
psql
or if I launch psql from the application drop down menu:
Machine stats:
OSX 10.8.4
psql (PostgreSQL) 9.2.4
Any help is appreciated.
I've also attempted to install PostgreSql via homebrew and I'm getting the same issue. I've also read the applications documentation page that states:
When Postgres.app first starts up, it creates the $USER database,
which is the default database for psql when none is specified. The
default user is $USER, with no password.
So it would seem the application is not creating $USER however I've installed->uninstalled-reinstalled several times now so it must be something with my machine.
I found the answer but I'm not sure exactly how it works as the user who answered on this thread -> Getting Postgresql Running In Mac: Database "postgres" does not exist didn't follow up. I used the following command to get psql to open:
psql -d template1
I'll leave this one unanswered until someone can provide an explanation for why this works.
It appears that your package manager failed to create the database named $user for you. The reason that
psql -d template1
works for you is that template1 is a database created by postgres itself, and is present on all installations.
You are apparently able to log in to template1, so you must have some rights assigned to you by the database. Try this at a shell prompt:
createdb
and then see if you can log in again with
psql -h localhost
This will simply create a database for your login user, which I think is what you are looking for. If createdb fails, then you don't have enough rights to make your own database, and you will have to figure out how to fix the homebrew package.
From the terminal, just Run the command on your command prompt window. (Not inside psql).
createdb <user>
And then try to run postgres again.
By default, postgres tries to connect to a database with the same name as your user. To prevent this default behaviour, just specify user and database:
psql -U Username DatabaseName
Login as default user: sudo -i -u postgres
Create new User: createuser --interactive
When prompted for role name, enter linux username, and select Yes to superuser question.
Still logged in as postgres user, create a database: createdb <username_from_step_3>
Confirm error(s) are gone by entering: psql at the command prompt.
Output should show psql (x.x.x) Type "help" for help.
Login using default template1 database:
#psql -d template1
#template1=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+---------+----------+-------------+-------------+---------------------
postgres | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca +
| | | | | gogasca=CTc/gogasca
template1 | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca +
| | | | | gogasca=CTc/gogasca
(3 rows)
Create a database with your userId:
template1=# CREATE DATABASE gogasca WITH OWNER gogasca ENCODING 'UTF8';
CREATE DATABASE
Quit and then login again
template1=# \q
gonzo:~ gogasca$ psql -h localhost
psql (9.4.0)
Type "help" for help.
gogasca=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+---------+----------+-------------+-------------+---------------------
gogasca | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca +
| | | | | gogasca=CTc/gogasca
template1 | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca +
| | | | | gogasca=CTc/gogasca
(4 rows)
I faced the same error when I trying to open postgresql on mac
psql: FATAL: database "user" does not exist
I found this simple command to solve it:
method1
$ createdb --owner=postgres --encoding=utf8 user
and type
psql
Method 2:
psql -d postgres
Post installation of postgres, in my case version is 12.2, I did run the below command createdb.
$ createdb `whoami`
$ psql
psql (12.2)
Type "help" for help.
macuser=#
Step 1:
psql -d template1
now you should be on psql terminal
Step 2:
CREATE DATABASE username;
make sure you use semicolon (;) after the database name;
optional: on psql terminal type \ls or \l to list all the databases;
Step 3:
psql -h localhost
now you should be connected;
Try using-
psql -d postgres
I was also facing the same issue when I ran psql
Had the same problem, a simple psql -d postgres did it (Type the command in the terminal)
This error can also occur if the environment variable PGDATABASE is set to the name of a database that does not exist.
On OSX, I saw the following error while trying to launch psql from the Postgress.app menu:
psql: FATAL: database "otherdb" does not exist
The solution to the error was to remove export PGDATABASE=otherdb from ~/.bash_profile:
Further, if PGUSER is set to something other than your username, the following error will occur:
psql: FATAL: role "note" does not exist
The solution is to remove export PGUSER=notme from ~/.bash_profile.
Not sure if it is already added in the answers, Anatolii Stepaniuk answer was very helpful which is the following.
psql -U Username postgres # when you have no databases yet
As the createdb documentation states:
The first database is always created by the initdb command when the data storage area is initialized... This database is called postgres.
So if certain OS/postgresql distributions do that differently, it is certainly not the default/standard (just verified that initdb on openSUSE 13.1 creates the DB "postgres", but not "<user>"). Long story short, psql -d postgres is expected to be used when using a user other than "postgres".
Obviously the accepted answer, running createdb to create a DB named like the user, works as well, but creates a superfluous DB.
Since this question is the first in search results, I'll put a different solution for a different problem here anyway, in order not to have a duplicate title.
The same error message can come up when running a query file in psql without specifying a database. Since there is no use statement in postgresql, we have to specify the database on the command line, for example:
psql -d db_name -f query_file.sql
First off, it's helpful to create a database named the same as your current use, to prevent the error when you just want to use the default database and create new tables without declaring the name of a db explicitly.
Replace "skynotify" with your username:
psql -d postgres -c "CREATE DATABASE skynotify ENCODING 'UTF-8';"
-d explicitly declares which database to use as the default for SQL statements that don't explicitly include a db name during this interactive session.
BASICS FOR GETTING A CLEAR PICTURE OF WHAT YOUR PostgresQL SERVER has in it.
You must connect to an existing database to use psql interactively. Fortunately, you can ask psql for a list of databases:
psql -l
.
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------------------------------+-----------+----------+-------------+-------------+-------------------
skynotify | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
myapp_dev | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
ruby-getting-started_development | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/skynotify +
| | | | | skynotify=CTc/skynotify
template1 | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/skynotify +
| | | | | skynotify=CTc/skynotify
(6 rows)
This does NOT start the interactive console, it just outputs a text based table to the terminal.
As another answers says, postgres is always created, so you should use it as your failsafe database when you just want to get the console started to work on other databases. If it isn't there, then list the databases and then use any one of them.
In a similar fashion, select tables from a database:
psql -d postgres -c "\dt;"
My "postgres" database has no tables, but any database that does will output a text based table to the terminal (standard out).
And for completeness, we can select all rows from a table too:
psql -d ruby-getting-started_development -c "SELECT * FROM widgets;"
.
id | name | description | stock | created_at | updated_at
----+------+-------------+-------+------------+------------
(0 rows)
Even if there are zero rows returned, you'll get the field names.
If your tables have more than a dozen rows, or you're not sure, it'll be more useful to start with a count of rows to understand how much data is in your database:
psql -d ruby-getting-started_development -c "SELECT count(*) FROM widgets;"
.
count
-------
0
(1 row)
And don't that that "1 row" confuse you, it just represents how many rows are returned by the query, but the 1 row contains the count you want, which is 0 in this example.
NOTE: a db created without an owner defined will be owned by the current user.
had the problem with using the JDBC driver, so one just has to add the database (maybe redundantly depending on the tool you may use) after the host name in the URL, e.g.
jdbc:postgres://<host(:port)>/<db-name>
further details are documented here: http://www.postgresql.org/docs/7.4/static/jdbc-use.html#JDBC-CONNECT
Connect to postgres via existing superuser.
Create a Database by the name of user you are connecting through to postgres.
create database username;
Now try to connect via username
This worked for me when solving this problem
i ran sudo -i -u postgress --> to gain access to my postgres database.
Then enter your password.
it would allow you to now enter psql
which would prompt you for other command
Cheers!
Had this problem when installing postgresql via homebrew.
Had to create the default "postgres" super user with:
createuser --interactive postgres answer y to for super user
createuser --interactive user answer y to for super user
you can set the database name you want to connect to in env variable PGDATABASE=database_name. If you dont set this psql default database name is as username. after setting this you don't have to createdb
Was running postgres in docker. In cli I was getting the error "root" role doesn't exist.
su - postgres
psql
solved the problem.
PostgreSQL has its own user on the system which is created when PostgreSQL is installed. The postgres user is able to log into PostgreSQL without using a password. No other user is able to log into PostgreSQL.
This means that before using PostgreSQL, you will need to switch to that user account with the command:
su - postgres
You will then be able to log into the PosgreSQL client with the command:
psql
You will not be able to access the database from the command line as any other user.
I still had the issue above after installing postgresql using homebrew - I resolved it by putting /usr/local/bin in my path before /usr/bin
This is a basic misunderstanding. Simply typing:
pgres
will result in this response:
pgres <db_name>
It will succeed without error if the user has the permissions to access the db.
One can go into the details of the exported environment variables but that's unnecessary .. this is too basic to fail for any other reason.

psql saying database does not exist but it does exist in pgadmin

I have just installed Postgres v10.4 on Windows and created, using pgadmin, a new database called analysis. It is there, I can see it in pgadmin, and it has one table in it. However, I cannot connect to this database using psql.
C:\WINDOWS\system32>psql -d postgres -U postgres
psql (10.4)
WARNING: Console code page (850) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
postgres=# l
postgres-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-----------------------------+-----------------------------+-----------------------
postgres | postgres | UTF8 | English_United Kingdom.1252 | English_United Kingdom.1252 |
template0 | postgres | UTF8 | English_United Kingdom.1252 | English_United Kingdom.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United Kingdom.1252 | English_United Kingdom.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres-# \q
C:\WINDOWS\system32>psql -d analysis -U postgres
psql: FATAL: database "analysis" does not exist
C:\WINDOWS\system32>
I don't know what I'm doing with psql, but all I've found is that I can connect to db postgres as user postgres. There's a \l command and it shows 3 databases (2 of which, template0 and template1) I can't even see in pgadmin.
How can I connect to the database (analysis) that I've just created in pgadmin? Can anybody explain what user I'm logged in under in pgadmin, if it's different to 'postgres' and if it isn't, how is it that I can't see in psql what I can see in pgadmin?
template0 and template1 are know as skeleton databases. When you use CREATE DATABASE command postgres copy the existing databae.
By default template1 database is used to create new database.
I think your pgadmin and psql are connected to different cluster. A cluster in postgresql is collection of one or more databases in a single instance of server
The image you posted does not show the complete view of pgadmin browser. But
if there are 2 cluster then you can see in pgadmin like "Servers(2)". To find running port of each cluster right click on corresponding cluster and select properties then click connection tab. Here port number can be seen.
Then connect to cluster in psql using that Port.

GitLab setup using official PostgreSQL and GitLab docker container not working

I am trying to setup GitLab using separate docker containers for both GitLab and PostgreSQL. I am using RancherOS v1.0.3 with Kubernetes enabled. For now, all i want is to have a single node having both the containers. Later, i will look into configuring them on separate nodes.
[rancher#rancher-agent-2 ~]$ cat postgresql.sh
docker run --name=postgresql -d \
--env 'DB_NAME=gitlabhq_production' \
--env 'DB_USER=gitlab' --env 'DB_PASS=password' \
--env 'DB_EXTENSION=pg_trgm' \
--volume /opt/postgresql:/var/lib/postgresql \
postgres:9.6.3
[rancher#rancher-agent-2 ~]$ cat gitlab.sh
docker run --name=gitlab -d --link postgresql:postgresql \
-v /opt/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce:9.3.9-ce.0
Now when i run the PostgreSQL container and try logging in, i get errors.
[rancher#rancher-agent-2 ~]$ docker exec -it postgresql bash
root#a6cef780c594:/# psql -d gitlabhq_production -U gitlab
psql: FATAL: role "gitlab" does not exist
Seems the db (gitlabhq_production) has not been created.
root#a6cef780c594:/# psql -U postgres
psql (9.6.3)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
Why is it not creating the db despite passing the parameters?
UPDATE:
PostgreSQL container log:
[rancher#rancher-agent-2 ~]$ docker logs postgresql
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/lib/postgresql/data ... ok
...
...
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
done
server started
ALTER ROLE
/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
...
...
You are using the wrong environment vars names:
DB_NAME should be POSTGRES_DB
DB_USER should be POSTGRES_USER
DB_PASS should be POSTGRES_PASSWORD
Check https://hub.docker.com/_/postgres/

psql: FATAL: database "<user>" does not exist

I'm using the PostgreSql app for mac (http://postgresapp.com/). I've used it in the past on other machines but it's giving me some trouble when installing on my macbook. I've installed the application and I ran:
psql -h localhost
It returns:
psql: FATAL: database "<user>" does not exist
It seems I can't even run the console to create the database that it's attempting to find. The same thing happens when I just run:
psql
or if I launch psql from the application drop down menu:
Machine stats:
OSX 10.8.4
psql (PostgreSQL) 9.2.4
Any help is appreciated.
I've also attempted to install PostgreSql via homebrew and I'm getting the same issue. I've also read the applications documentation page that states:
When Postgres.app first starts up, it creates the $USER database,
which is the default database for psql when none is specified. The
default user is $USER, with no password.
So it would seem the application is not creating $USER however I've installed->uninstalled-reinstalled several times now so it must be something with my machine.
I found the answer but I'm not sure exactly how it works as the user who answered on this thread -> Getting Postgresql Running In Mac: Database "postgres" does not exist didn't follow up. I used the following command to get psql to open:
psql -d template1
I'll leave this one unanswered until someone can provide an explanation for why this works.
It appears that your package manager failed to create the database named $user for you. The reason that
psql -d template1
works for you is that template1 is a database created by postgres itself, and is present on all installations.
You are apparently able to log in to template1, so you must have some rights assigned to you by the database. Try this at a shell prompt:
createdb
and then see if you can log in again with
psql -h localhost
This will simply create a database for your login user, which I think is what you are looking for. If createdb fails, then you don't have enough rights to make your own database, and you will have to figure out how to fix the homebrew package.
From the terminal, just Run the command on your command prompt window. (Not inside psql).
createdb <user>
And then try to run postgres again.
By default, postgres tries to connect to a database with the same name as your user. To prevent this default behaviour, just specify user and database:
psql -U Username DatabaseName
Login as default user: sudo -i -u postgres
Create new User: createuser --interactive
When prompted for role name, enter linux username, and select Yes to superuser question.
Still logged in as postgres user, create a database: createdb <username_from_step_3>
Confirm error(s) are gone by entering: psql at the command prompt.
Output should show psql (x.x.x) Type "help" for help.
Login using default template1 database:
#psql -d template1
#template1=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+---------+----------+-------------+-------------+---------------------
postgres | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca +
| | | | | gogasca=CTc/gogasca
template1 | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca +
| | | | | gogasca=CTc/gogasca
(3 rows)
Create a database with your userId:
template1=# CREATE DATABASE gogasca WITH OWNER gogasca ENCODING 'UTF8';
CREATE DATABASE
Quit and then login again
template1=# \q
gonzo:~ gogasca$ psql -h localhost
psql (9.4.0)
Type "help" for help.
gogasca=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+---------+----------+-------------+-------------+---------------------
gogasca | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca +
| | | | | gogasca=CTc/gogasca
template1 | gogasca | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca +
| | | | | gogasca=CTc/gogasca
(4 rows)
I faced the same error when I trying to open postgresql on mac
psql: FATAL: database "user" does not exist
I found this simple command to solve it:
method1
$ createdb --owner=postgres --encoding=utf8 user
and type
psql
Method 2:
psql -d postgres
Post installation of postgres, in my case version is 12.2, I did run the below command createdb.
$ createdb `whoami`
$ psql
psql (12.2)
Type "help" for help.
macuser=#
Step 1:
psql -d template1
now you should be on psql terminal
Step 2:
CREATE DATABASE username;
make sure you use semicolon (;) after the database name;
optional: on psql terminal type \ls or \l to list all the databases;
Step 3:
psql -h localhost
now you should be connected;
Try using-
psql -d postgres
I was also facing the same issue when I ran psql
Had the same problem, a simple psql -d postgres did it (Type the command in the terminal)
This error can also occur if the environment variable PGDATABASE is set to the name of a database that does not exist.
On OSX, I saw the following error while trying to launch psql from the Postgress.app menu:
psql: FATAL: database "otherdb" does not exist
The solution to the error was to remove export PGDATABASE=otherdb from ~/.bash_profile:
Further, if PGUSER is set to something other than your username, the following error will occur:
psql: FATAL: role "note" does not exist
The solution is to remove export PGUSER=notme from ~/.bash_profile.
Not sure if it is already added in the answers, Anatolii Stepaniuk answer was very helpful which is the following.
psql -U Username postgres # when you have no databases yet
As the createdb documentation states:
The first database is always created by the initdb command when the data storage area is initialized... This database is called postgres.
So if certain OS/postgresql distributions do that differently, it is certainly not the default/standard (just verified that initdb on openSUSE 13.1 creates the DB "postgres", but not "<user>"). Long story short, psql -d postgres is expected to be used when using a user other than "postgres".
Obviously the accepted answer, running createdb to create a DB named like the user, works as well, but creates a superfluous DB.
Since this question is the first in search results, I'll put a different solution for a different problem here anyway, in order not to have a duplicate title.
The same error message can come up when running a query file in psql without specifying a database. Since there is no use statement in postgresql, we have to specify the database on the command line, for example:
psql -d db_name -f query_file.sql
First off, it's helpful to create a database named the same as your current use, to prevent the error when you just want to use the default database and create new tables without declaring the name of a db explicitly.
Replace "skynotify" with your username:
psql -d postgres -c "CREATE DATABASE skynotify ENCODING 'UTF-8';"
-d explicitly declares which database to use as the default for SQL statements that don't explicitly include a db name during this interactive session.
BASICS FOR GETTING A CLEAR PICTURE OF WHAT YOUR PostgresQL SERVER has in it.
You must connect to an existing database to use psql interactively. Fortunately, you can ask psql for a list of databases:
psql -l
.
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------------------------------+-----------+----------+-------------+-------------+-------------------
skynotify | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
myapp_dev | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
ruby-getting-started_development | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/skynotify +
| | | | | skynotify=CTc/skynotify
template1 | skynotify | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/skynotify +
| | | | | skynotify=CTc/skynotify
(6 rows)
This does NOT start the interactive console, it just outputs a text based table to the terminal.
As another answers says, postgres is always created, so you should use it as your failsafe database when you just want to get the console started to work on other databases. If it isn't there, then list the databases and then use any one of them.
In a similar fashion, select tables from a database:
psql -d postgres -c "\dt;"
My "postgres" database has no tables, but any database that does will output a text based table to the terminal (standard out).
And for completeness, we can select all rows from a table too:
psql -d ruby-getting-started_development -c "SELECT * FROM widgets;"
.
id | name | description | stock | created_at | updated_at
----+------+-------------+-------+------------+------------
(0 rows)
Even if there are zero rows returned, you'll get the field names.
If your tables have more than a dozen rows, or you're not sure, it'll be more useful to start with a count of rows to understand how much data is in your database:
psql -d ruby-getting-started_development -c "SELECT count(*) FROM widgets;"
.
count
-------
0
(1 row)
And don't that that "1 row" confuse you, it just represents how many rows are returned by the query, but the 1 row contains the count you want, which is 0 in this example.
NOTE: a db created without an owner defined will be owned by the current user.
had the problem with using the JDBC driver, so one just has to add the database (maybe redundantly depending on the tool you may use) after the host name in the URL, e.g.
jdbc:postgres://<host(:port)>/<db-name>
further details are documented here: http://www.postgresql.org/docs/7.4/static/jdbc-use.html#JDBC-CONNECT
Connect to postgres via existing superuser.
Create a Database by the name of user you are connecting through to postgres.
create database username;
Now try to connect via username
This worked for me when solving this problem
i ran sudo -i -u postgress --> to gain access to my postgres database.
Then enter your password.
it would allow you to now enter psql
which would prompt you for other command
Cheers!
Had this problem when installing postgresql via homebrew.
Had to create the default "postgres" super user with:
createuser --interactive postgres answer y to for super user
createuser --interactive user answer y to for super user
you can set the database name you want to connect to in env variable PGDATABASE=database_name. If you dont set this psql default database name is as username. after setting this you don't have to createdb
Was running postgres in docker. In cli I was getting the error "root" role doesn't exist.
su - postgres
psql
solved the problem.
PostgreSQL has its own user on the system which is created when PostgreSQL is installed. The postgres user is able to log into PostgreSQL without using a password. No other user is able to log into PostgreSQL.
This means that before using PostgreSQL, you will need to switch to that user account with the command:
su - postgres
You will then be able to log into the PosgreSQL client with the command:
psql
You will not be able to access the database from the command line as any other user.
I still had the issue above after installing postgresql using homebrew - I resolved it by putting /usr/local/bin in my path before /usr/bin
This is a basic misunderstanding. Simply typing:
pgres
will result in this response:
pgres <db_name>
It will succeed without error if the user has the permissions to access the db.
One can go into the details of the exported environment variables but that's unnecessary .. this is too basic to fail for any other reason.