Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a valid PostgreSQL database name?

Tags:

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?

like image 585
dan Avatar asked Apr 18 '12 19:04

dan


People also ask

What is postgres database name?

Switching Databases Most Postgres servers have three databases defined by default: template0 , template1 and postgres . template0 and template1 are skeleton databases that are or can be used by the CREATE DATABASE command. postgres is the default database you will connect to before you have created any other databases.

How do I find my postgres database?

Use \l or \l+ in psql to show all databases in the current PostgreSQL server. Use the SELECT statement to query data from the pg_database to get all databases.

How do you name a database?

For the traditional naming convention: Database names must only consist of the letters a to z (both lower and upper case allowed), the numbers 0 to 9 , and the underscore ( _ ) or dash ( - ) symbols. This also means that any non-ASCII database names are not allowed. Database names must always start with a letter.

How do I find my PostgreSQL instance name?

Open the RDS console and then choose Databases to display a list of your DB instances. Choose the PostgreSQL DB instance name to display its details.


2 Answers

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.

like image 59
kgrittn Avatar answered Oct 03 '22 21:10

kgrittn


Try putting it in double quotes:

ALTER DATABASE "my-database" SET SCHEMA = myschema,public;

like image 32
Adam M. Avatar answered Oct 03 '22 21:10

Adam M.