Psql error database 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 applicat...

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.

REVSYS

  • About
  • Impact
  • Products
  • Blog
  • Contact

Common PostgreSQL Error Messages and Possible Solutions

by Frank Wiles

Below is a resource I’ve put together for newer PostgreSQL database users. Listed are some of the most common
error messages you may encounter. While the error messages do in fact
tell you exactly what is causing the problem, I find that
users often don’t know what next steps to take in resolving the issue.

If you there is a common error you would like me to include in this list or
you find anything inaccurate on this page, please drop me an E-mail at
info@revsys.com.

List of Errors

  • psql: FATAL: database «root» does not exist
  • psql: could not connect to server: No such file or directory Is the
    server running locally and accepting connections on Unix domain
    socket «/tmp/.s.PGSQL.5432»?
  • psql: could not connect to server: Connection refused Is the server
    running on host «192.168.0.1» and accepting TCP/IP connections on
    port 5432?
  • user X has no permission for table Y

Error:
psql: FATAL: database «root» does not exist

Common Cause: A database named ‘root’ does not exist on
your system

This error trips up new PostgreSQL users quite often. When you simply
run psql from the command line it, by default, attempts to
log you into a database with the same name as your current Unix user
name. In this case that is ‘root’, but it could be ‘postgres’, or
‘bob’.

If you are setting up your database for the first time, you will need
to become the PostgreSQL user ( typically ‘postgres’ ) which can be
accomplished by either:

  • logging in as that user
  • su‘ing to root and as root su‘ing
    to the postgres user

Once you are the postgres user you will need to setup one or more
databases and some users. See the following articles for more
information on this:

  • Common PostgreSQL Problem this blog post describes this error in more detail
  • Database Roles and Privileges The official PostgreSQL documentation on setting up users and access permissions
  • Managing Databases The official docs on creating and maintaining databases

Error:
psql: could not connect to server: No such file or directory Is the
server running locally and accepting connections on Unix domain
socket «/tmp/.s.PGSQL.5432»?

Common Cause: The postmaster or PostgreSQL’s
server daemon process is not running.

Typically this error means PostgreSQL is not currently running on
the system you are logged into. You will need to start the daemon,
which is typically done through your distribution’s init system.

The easiest way to determine if PostgreSQL is running is to look for
it using ps. For example when I run the command on my
Fedora system:

ps auxw | grep post

I see the following processes:

[frank@host:~]$ ps auxw | grep post
postgres  4335  2.4 27704  2936 14:47 /usr/bin/postmaster -p 5432 -D /var/lib/pgsql/data
postgres  4337  0.0  9972   548 14:47 postgres: logger process
postgres  4339  0.0 27704   804 14:47 postgres: writer process
postgres  4340  0.0 10972   544 14:47 postgres: stats buffer process
postgres  4341  0.0 10148   668 14:47 postgres: stats collector process
frank     4346  0.0  3912   676 14:47 grep post
[frank@host:~]$

Which as you can see means PostgreSQL is running on my local system. If
we had received no results or just the final result line then we would
know for certain that it is not running on this system.

Error:
psql: could not connect to server: Connection refused Is the server
running on host «192.168.0.1» and accepting TCP/IP connections on
port 5432?

Common Cause: The postmaster or PostgreSQL’s
server daemon process is not running or configured incorrectly.

When you receive this error most of the time it is due to not having
PostgreSQL configured to allow TCP/IP connections or at least not
connections from your particular workstation. If you have verified that
postmaster is indeed running on the host you are trying to
connect to then here is a list of common causes to this problem:

  • postgresql.conf not setup to allow TCP/IP
    connections. You’ll want to look at the
    listen_address configuration parameter.
  • postgresql.conf is not setup to allow
    connections on a non-standard port number. To determine this
    look at the port configuration option.
  • authentication rules in PostgreSQL’s access configuration
    file (pg_hba.conf) are not setup to allow
    either your uses or IP address to connect to that database.
    See the official documentation for more information on
    setting up your pg_hba.conf properly.
  • ensure that there are no firewalls, such as iptables
    that are keeping your local system from even establishing a
    connection to the remote host

Error:
user X has no permission for table Y

Common Cause: You are not the owner of the table or
the owner has not granted you the proper permissions

Depending on what sort of
authentication methods (trust, md5, etc. ) you
have setup, you might receive the following error when attempting
to work with a particular table. This is usually caused by creating
the table as say user ‘postgres’, and then attempting to use it as
user ‘bob’. There are two possible solutions to this problem:

  • Change the ownership of the table to the user you need.
    You will need to login as the current owner or a superuser
    account and execute ALTER TABLE table_name OWNER TO new_owner_name
  • You can also GRANT other users to access this account with GRANT ALL ON table_name TO user_name. Please note this will give the user full access to select, insert, update, and delete from this table. You can limit their access to SELECT access with GRANT SELECT ON table_name TO user_name

For more information please see the following documentation:

  • PostgreSQL roles and privileges explains users, roles, and database privileges
  • PostgreSQL authentication
  • ALTER TABLE command reference
  • GRANT command reference

Thanks go to Jacob Kaplan-Moss for
suggesting this error be included here.

Frank Wiles has been an avid user of
PostgreSQL for over 10 years.
Revolution Systems provides PostgreSQL Optimizations and commercial support of PostgreSQL.

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.

Already on GitHub?
Sign in
to your account

Comments

@bingomanatee

using the «psql» menu item (OSX 10.8.4) I get

Last login: Sat Oct 19 07:59:28 on ttys009
/Applications/Postgres.app/Contents/MacOS/bin/psql ; exit;
Daves-MacBook-Pro:~ dave$ /Applications/Postgres.app/Contents/MacOS/bin/psql ; exit;
psql: FATAL: database «dave» does not exist
logout

[Process completed]

@jakob

The first time you open Postgres.app, it automatically creates a database named like your username. Apparently this database doesn’t exist.

There are a few possible reasons why this database doesn’t exist:

  1. You changed your username
  2. psql is not connecting to Postgres.app but to a different server

There are a number of reasons why psql might be connecting to a different server:

  1. Maybe you have a different Postgres installation already running on your Mac
  2. Maybe you already started a copy of Postgres.app as a different user on your machine
  3. Maybe you changed the psql configuration using environment variables PGDATABASE, PGHOST, PGPORT, PGUSER or you changed settings in the file ~/.psqlrc

@Shpigford

I’m also getting this error. I’m using the 9.3.1.0-alpha1 pre-release. This is on OS X Mavericks.

$ which psql
/Applications/Postgres93.app/Contents/MacOS/bin/psql

$ psql
psql: FATAL: database "USERNAME" does not exist

I haven’t changed my username. My previous Postgres installation isn’t running (I force quit all instance of Postgres).

@jakob

Could you try connecting to the «postgres» database:
psql -d postgres
and then execute the following command:
show data_directory;
On 9.3.1.0-alpha1 that should return /Users/USERNAME/Library/Application Support/Postgres93/var
Then check which databases exist on the server using the shorthand l

If all that works, it seems that Postgresapp called initdb successfully, but createdb failed. You can do that manually, just execute createdb USERNAME from the terminal (not inside psql)

krand7, jpbamberg1993, gabrielsiedler, dolefir, dineshPallapa, aarontaddiken, attilaRacz, andreis, szpasztor, Matthew-Jimenez, and 18 more reacted with thumbs up emoji
ggrig93, KehindeAyan, MilaSinica, aspmonk, and senoodle reacted with heart emoji

@dylanjha

also happened to me on a virgin Mavericks install. Had to createdb USERNAME myself.

PS. thanks for maintaining Postgres.app, I love it.

@mcandre

This bug occurs for me as well using the Homebrew formula for installing PostgreSQL.

Trace:

$ brew install postgresql
...
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
$ psql
psql: FATAL:  database "andrew" does not exist

System:

$ specs postgres brew os
Specs:

specs 0.8
https://github.com/mcandre/specs#readme

postgres --version
postgres (PostgreSQL) 9.3.4

brew --version
0.9.5

system_profiler SPSoftwareDataType | grep 'System Version'
      System Version: OS X 10.9.3 (13D65)

@ghost

happened to me as well on a Yosemite install while installing a rails app locally.

FATAL: database "USERNAME" does not exist

createdb USERNAME fixed the problem — thanks!

@raychenon

I first tried with Homebrew then I ran into this error. Then uninstalled postgres by brew
I installed PostgresApp, this error showed up again.
How can you check if you have previous Postgres installed on the mac ?
I use

which psql
but it will show me /Applications/Postgres.app/Contents/Versions/9.4/bin/psql

@kenju254

is there a solution to this yet ? I am getting the same error

@yangman-c

just do like this:
step 1: createdb
step 2: psql -h localhost or psql

hope useful to you.

@kultgenj

When I type createdb USERNAME i get the response command not found, could you assist me?

@jakob

  1. First, make sure you remove older intallations of PostgreSQL before starting Postgres.app. To do so, follow the instructions here: http://postgresapp.com/documentation/remove.html
  2. Restart your Mac after uninstalling other PostgreSQL installations, and open Activity Monitor to make sure there are no processes named «postgres» running.
  3. Now open Postgres.app. If it still doesn’t work, try using the createdb USERNAME command in Terminal.
  4. Make sure you set up your $PATH correctly: http://postgresapp.com/documentation/cli-tools.html

jakob

added a commit
that referenced
this issue

Dec 23, 2016

@jakob

This should help people fix issues like #139

@jakob

The new version of Postgres.app now checks if the port is in use before attempting to init a server and create a database. This makes it much less likely to fail. Additionally, Postgres.app displays more reasonable error messages when a problem occurs.

And finally, the troubleshooting section in the docs now explains how to fix this problem.

@AgoniNemo

createdb -h localhost -p 5432 -U <USERNAME> testDB

psql testDB

@sarkarchandan

For me as well, «database doesn’t exist» error was showing up. While trouble shooting I figured that I accidentally dropped the database with my name that Postgresapp creates while initialising.

I simply created one database with my name and things were fine. Thus, I assume that every time I launch the Postgresapp it checks connection for the database with my name. Please correct me if I am wrong.

Thanks in advance.

@monroe100

Понравилась статья? Поделить с друзьями:
  • Psql error connection to server on socket var run postgresql s pgsql 5432
  • Proxysql error access denied for user
  • Production error no application encryption key has been specified
  • Procedure too large vba ошибка как исправить
  • Problem to run or locate the batch file install cmd как исправить