How do I change the default client_encoding in Postgres? - postgresql

I'm trying to change the default value for the client_encoding configuration variable for a PostgreSQL database I'm running. I want it to be UTF8, but currently it's getting set to LATIN1.
The database is already set to use UTF8 encoding:
application_database=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------------------+----------+----------+-------------+-------------+--------------------------------------
postgres | postgres | LATIN1 | en_US | en_US |
application_database | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres +
| | | | | application_database=Tc/postgres
template0 | postgres | LATIN1 | en_US | en_US | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | LATIN1 | en_US | en_US | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
Which according to the docs should already result in the client using UTF8 as its default client_encoding (emphasis mine):
client_encoding (string)
Sets the client-side encoding (character set). The default is to use the database encoding.
But it doesn't:
$ sudo psql --dbname=application_database
psql (9.1.19)
Type "help" for help.
application_database=# SHOW client_encoding;
client_encoding
-----------------
LATIN1
(1 row)
I even tried using ALTER USER <user> SET ... to change the default config for the user I'm logging in as:
application_database=# ALTER USER root SET client_encoding='UTF8';
ALTER ROLE
application_database=# SELECT usename, useconfig FROM pg_shadow;
usename | useconfig
----------------------+------------------------
postgres |
root | {client_encoding=UTF8}
application_database |
(3 rows)
But that also had no effect:
$ sudo psql --dbname=application_database
psql (9.1.19)
Type "help" for help.
application_database=# SELECT current_user;
current_user
--------------
root
(1 row)
application_database=# SHOW client_encoding;
client_encoding
-----------------
LATIN1
(1 row)
There's nothing in any of the PSQL files on my system:
vagrant#app-database:~$ cat ~/.psqlrc
cat: /home/vagrant/.psqlrc: No such file or directory
vagrant#app-database:~$ cat /etc/psqlrc
cat: /etc/psqlrc: No such file or directory
vagrant#app-database:~$ sudo su
root#app-database:/home/vagrant# cat ~/.psqlrc
cat: /root/.psqlrc: No such file or directory
I'm running PosgreSQL 9.1:
application_database=# SELECT version();
version
-------------------------------------------------------------------------------------------------------------
PostgreSQL 9.1.19 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3, 64-bit
(1 row)

Okay, so first things first: why isn't setting the user or database encoding having any effect?
Turns out it's because of this line from the psql documentation:
If at least one of standard input or standard output are a terminal, then psql sets the client encoding to "auto", which will detect the appropriate client encoding from the locale settings (LC_CTYPE environment variable on Unix systems). If this doesn't work out as expected, the client encoding can be overridden using the environment variable PGCLIENTENCODING.
So, in fact, your previous configuration changes actually have been working, just not in the interactive psql console. Try the following command:
sudo psql --dbname=application_database -c "SHOW client_encoding;" | cat
You should see that the client encoding is actually UTF8:
client_encoding
-----------------
UTF8
(1 row)
Now run the command again, but without piping it to cat:
sudo psql --dbname=application_database -c "SHOW client_encoding;"
You should get the result:
client_encoding
-----------------
LATIN1
(1 row)
So basically, psql is only using LATIN1 encoding for commands involving the terminal.
How can you fix this? Well, there are a few possible ways.
One would be to do as the docs suggest and set the PGCLIENTENCODING environment variable to UTF8 somewhere persistent, such as ~/.profile or ~/.bashrc to affect only your user, or /etc/environment to affect the whole system (see How to permanently set environmental variables).
Another option would be to configure your system locale settings to use en_US.utf8 instead of en_US (or equivalent). The method for doing this may vary depending on your system, but usually you can do it by modifying ~/.config/locale.conf (your user only) or /etc/default/locale or /etc/locale.conf (system-wide). This will affect more than just postgres, and I believe more closely addresses the root of the problem. You can check your current locale settings by running locale.
One other solution would be to update your psqlrc file to include SET client_encoding=UTF8;. That file is located at ~/.psqlrc (your user only) or /etc/psqlrc (system-wide). Note that this method won't affect the result of the command we were using to the client encoding earlier, since the docs state (emphasis mine):
--command=command
Specifies that psql is to execute one command string, command, and then exit. This is useful in shell scripts. Start-up files (psqlrc and ~/.psqlrc) are ignored with this option.

Did you set client_encoding in postgresql.conf (and reload config or restart)? Make sure it's UTF8 not utf8
What is the result of cat ~/.psqlrc and cat /etc/psqlrc ?
I know you're looking for server-side default, but on the client, you can set an OS envvar:
export PGCLIENTENCODING=UTF8
to do this for all users (on that machine), put that in /etc/profile

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.

Change Database Collation, Ctype in Postgresql

how do I change Collation, cType to - en_IN from en_US.UTF-8
List of databases
Name | Owner | Encoding | Collation | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
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
my current postgresversion is 8.4
ive installed it using
sudo apt-get install postgresql-8.4 postgresql-contrib-8.4
im doing this in my ubuntu amazon server ec2
It's not necessary to recreate the whole database cluster. You need however to recreate your database.
Run createdb with these options (man createdb):
-E encoding, --encoding=encoding
Specifies the character encoding scheme to be used in this
database. The character sets supported by the PostgreSQL server
are described in Section 22.3.1, “Supported Character Sets”, in
the documentation.
-l locale, --locale=locale
Specifies the locale to be used in this database. This is
equivalent to specifying both --lc-collate and --lc-ctype.
--lc-collate=locale
Specifies the LC_COLLATE setting to be used in this database.
--lc-ctype=locale
Specifies the LC_CTYPE setting to be used in this database.
It seems you really can't change the collation of an existing database:
=> ALTER DATABASE dbname SET "Collate" To Russian;
ERROR: unrecognized configuration parameter "Collate"
Note that you can set collation for a table or a column, see a good tutorial on collations in PostgreSQL.
My recommendation:
take a pg_dumpall
re-initialize the db cluster, making sure the locale information is correct
restore your dump.
I have found that sometimes it is possible that one may have to create a db with template template0 (-T template0 from bash or WITH TEMPLATE template0 from psql) to use a non-init-db locale.
I had to change to POSIX.UTF-8. I managed that with the following commands:
su postgres
psql
\l
update pg_database set datcollate='POSIX.UTF-8', datctype='POSIX.UTF-8' where datname='databasename';
\l
its Very very Simple Solutions.
Step1. su - postgres
Step2. psql
Setp3. update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = 'icinga'; (dont'forget to add ;)
Step4. \l to check

Chef ENV settings not working

I am attempting to set up postgresql on a Vagrant box using Chef solo and am running into some problems. I need the default postgres encoding/locale to be UTF8. By default, the locale of the precise64 Ubuntu box is set to "C" so postgres is using LATIN1 for encoding. This is what I've done so far:
I have a chef recipe that sets the locale by doing the following:
template "/etc/profile.d/lang.sh" do
source "lang.sh.erb"
mode "0644"
end
execute "locale-gen" do
command "locale-gen en_US.UTF-8"
end
execute "dpkg-reconfigure-locales" do
command "dpkg-reconfigure locales"
end
where lang.sh.erb looks like:
export LANGUAGE="en_US.UTF-8"
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
This sets up the locale correctly, but unfortunately it doesn't modify the current environment. So I have another recipe that just sets the ENV before including postgresql
ENV["LANGUAGE"] = ENV["LANG"] = ENV["LC_ALL"] = "en_US.UTF-8"
include_recipe "postgresql::server"
This has no effect. The locale is set up correctly:
postgres#precise64:~$ locale
LANG=en_US.UTF-8
LANGUAGE=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8
But postgres used the "C" locale when it was installed.
postgres#precise64:~$ psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | LATIN1 | en_US | en_US |
template0 | postgres | LATIN1 | en_US | en_US | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | LATIN1 | en_US | en_US | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
For attribution's sake, I got all this from http://www.softr.li/blog/2012/05/22/chef-recipe-to-install-a-postgresql-server-on-a-machine-configured-with-en_us-locales.
I found that the solution that worked for me was to either in the bootstrap shell script, or as inline shell, to copy the /etc/default/lang.sh to the box prior to any recipes being run. (So should be first thing done in the Vagrant file after box definitions)
lang file:
export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
From here the database should get setup with the UTF-8 encoding.
Hope this helps as I have spent days searching for solutions to this, and came up with the bits and pieces from various discussions, but realized that the problem was timing of when the values are set...
the environment variable doesn't work for chef resources.
According to the postgresql cookbook, you should set the attribute node['postgresql']['initdb_locale'] to setup the locale when initializing a database. For example, use this section under parent section name: serverin your .kitchen.yml:
attributes:
postgresql:
initdb_locale: "en_US.UTF_8"
You can drop and recreate the postgres template database as UTF-8 after the fact. Not a perfect solution, but it does work in your Chef recipe. See: http://www.pebra.net/blog/2013/06/10/when-struggling-with-postgresql-and-utf8-slash-latin/
include_recipe "postgresql::server"
include_recipe "database::postgresql"
execute "Psql template1 to UTF8" do
user "postgres"
command <<-SQL
echo "
UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
DROP DATABASE template1;
CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE' LC_CTYPE='en_US.utf8' LC_COLLATE='en_US.utf8';
UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
\\c template1
VACUUM FREEZE;" | psql postgres -t
SQL
# only_if '[ $(echo "select count(*) from pg_database where datname = \'template1\' and datcollate = \'en_US.utf8\'" |psql postgres -t) -eq 0 ]'
end

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.