What is a valid PostgreSQL database name? - postgresql

I create a database with a hyphen in the middle of the name with createdb. That successfully creates the database, but within the psql interactive client, I get a syntax error if I try a command like this:
ALTER DATABASE my-database SET SCHEMA = myschema,public;
psql complains of a syntax error at or near "-"
Is there some documentation for what counts as a valid PostgreSQL database name?
Should I just underscores instead of hyphens?

The documentation you asked about is here:
http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
Most people just stick to lowercase letters, numeric digits, and underscores -- to avoid typing the quotes all the time.

Try putting it in double quotes:
ALTER DATABASE "my-database" SET SCHEMA = myschema,public;

I faced one issue and above answers helped me. So sharing scenario on dbname
Scenario:I was tried to change database name using PG admin III. My database name was My_Database
running below queries failed:
ALTER DATABASE My_Database RENAME TO dba;
ALTER DATABASE [My_Database] RENAME TO dba;
ALTER DATABASE 'My_Database' RENAME TO dba;
Then i tried below and its successful
ALTER DATABASE "My_Database" RENAME TO dba;

Related

not able to delete database in postgres

The drop database command is not working in sql shell. It shows db doesn't exists. But pgadmin and \l shows the db. What is the problem and how to delete the db?
Try writing "myDatabase" (with quotation marks "") since your name is case-sensitive.
In general it is better to use lower-case names in postgres and I think in unix in general.

Why command \dt gives - no relations found?

I have created a database in postgres. It has 3 empty tables. The table has user tom as its Superuser along with root. I am logged in as tom and connected to mydb database. But still the commands \d or \dt - gives no relations found.
Is there any alternate to SHOW TABLE in postgresql?
Could not find solution here
I get results from - \dt *.*.
It is not a problem with your search_path, it could be an issue with your schema permissions as described in the answer here. Check with \dn+ that the public schema indicates permissions for the postgres role, and if not, grant them with: GRANT ALL ON SCHEMA public TO public;
I solved my problem by using double quote e.g \d "Table_name". Because my table name is capitalized like Foo, Bar. Hope that could help someones.
You might not be connected to the right database.
The first command \c DATABASE_NAME, coming from the following comment on Reddit, did the trick for me.
Make sure you're connected to the correct database with \c . Using \l will list all databases regardless of which database you're connected to, but most commands are specific to the connected database.
Use \dt .* to list all tables with name matching in all schemas.
Assuming the table exists, it will tell you what schema it's in. Then you can query with SELECT * FROM .;.
If you want to avoid having to explicitly specify the schema name, make sure the schema that contains the table is in your path: SHOW search_path and SET search_path .
If you have different instances of postgres running on server on different port it helps when you connect to database with specific port psql -p 5432 databasename( or psql -p 5433 databasename etc.)
you must have not added a semicolon at the end that's what always happens to me I always forget my semicolon -> create table nameoftable()
This happened for me when I removed the default privileges for PUBLIC on the public schema (specifically the USAGE privilege).
I had the exact same issue. None of the answers above helped. I was able to see my table when I ran \dt .*. Then I realized I had to call explicitly state its schema when running SELECT statements.
So, if you run SELECT * FROM public.<your_tablename_goes_here>; it should work. I hope this helps!

How can I create a capitalized Postgres role name?

I am getting the following error when I try to start my Rails app running a Postgres database:
ActiveRecord::NoDatabaseError
FATAL: role "Divergent" does not exist
To fix this I ran CREATE ROLE Divergent from inside the psql console but it only creates a lowercase divergent.
How then do I create a role name that matches the case of the name that the Postgres db expects me to have (i.e Divergent with a 'D')? Why does the Postgres db expect me to have this name and can I change it?
The problem was in config/database.yml. The username in my development database config was commented out and so postgres was defaulting to using my system username as the default role name. I simply uncommented the line and named it what I wanted. And then in the postgres console I created a role that corresponded to that username. That did the trick.
Quote the user name to have it be case sensitive. Note that you'll need to quote it again whenever you use it inside psql (like GRANT ALL ON table TO "Divergent").
You can also use the createuser command outside of psql, which is case sensitive: createuser Divergent.

Querying postgresql using psql command in bash throws ERROR: relation "testschema.testtable" does not exist

On running the below command in bash throws:
ERROR: relation "testschema.testtable" does not exist.
Command line:
psql -h "localhost" -d "postgres" -U "postgres" -c "select * from TestSchema.TestTable;"
Why is it searching relation rather than schema?
How can I set default schema to be searched, and can I set a list of multiple schemas from multiple databases as default list?
Why is it searching relation rather than schema?
TestSchema.TestTable is a relation (table). I is a fully qualified table name.
The reason it's not finding it, is most probably because you created the schema and the table using doublequotes:
create table "TestSchema"."TestTable" (...)
which makes the names case sensitive and forces you to always use quotes to qualify the name. Please see the manual for details about "delimited identifiers" (aka "quoted identifiers):
https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
How can I set default schema to be searched
See the "set schema" statement: https://www.postgresql.org/docs/current/sql-set.html
You can also define a default schema using the ALTER USER command.
can I set a list of multiple schemas from multiple databases as default list
Yes, set the search_path configuration variable: http://www.postgresql.org/docs/current/static/runtime-config-client.html#GUC-SEARCH-PATH
This can also be set permanently through an ALTER USER statement.

rename database in psql

Could anybody help me to rename database in postgresql from the Linux shell
ALTER DATABASE name RENAME TO newname
The above statement doesn't execute
This may be a stupidly obvious question. Are you running psql as the postgres user?
e.g.
$ sudo -u postgres psql
# alter database FOO rename to BAR;
# \q
Which version of postgresql? From the 8.1 Documentation:
ALTER DATABASE name RENAME TO newname;
Only the database owner or a superuser
can rename a database; non-superuser
owners must also have the CREATEDB
privilege. The current database cannot
be renamed. (Connect to a different
database if you need to do that.)
You might need privileges to rename db. Only db owner or super user can do that, owner also needs a createdb privilege.
Also the database you're connected to cannot be renamed, you need to connect to a different one
You cannot rename a database you are connected to. Make sure you are disconnected before changing the dbname.
In PGAdmin, you can just right-click on the database itself, go to properties, and rename it from there.
As others have pointed out, you may also try the command :
ALTER DATABASE (DB NAME) RENAME TO (NEW DB NAME);
Disconnect database (Ctrl + F2 in DataGrip)
And then:
$ psql -U postgres
postgres=# ALTER DATABASE db_a RENAME TO db_b;
GL
Below given are the steps for renaming the database in postgresql.
1) Right click on database and choose refresh.
2) Right click again and choose properties option.
3) Under the properties tab, you can change the name with the one that you desire.