Search code examples
c#.netpostgresqlnpgsql

Can't create table with PostgreSQL including "#" (npgsql.NET)


So I've actually got two issues with PostgreSQL. I actually use npgsql.NET to create queries, connections, other with PostgreSQL, however I am new to this database software.

First Issue

I got the error:

Npgsql.PostgresException: '42601: syntax error at or near "#"

POSITION: 16'

after using the script:

CREATE TABLE {textBox1.Text} (
    user_id serial PRIMARY KEY,
    username VARCHAR ( 50 ) UNIQUE NOT NULL,
    password VARCHAR ( 50 ) NOT NULL,
    email VARCHAR ( 255 ) UNIQUE NOT NULL,
    created_on TIMESTAMP NOT NULL,
        last_login TIMESTAMP
);

The textbox1.Text included: Pronner#2223.

Second Issue

When creating a table with the name PRONNER for example, it shows up as pronner. What's wrong with the capitalization system? Or can it possibly be because I'm using pgAdmin 4 so I just see it as lower case there?

I'm quite new to this like I mentioned at the beginning of the issue, and I used to use MySQL so the syntax is a teensy bit different, but the system is very different.


Solution

  • The doc says

    There is a second kind of identifier: the delimited identifier or quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes ("). A delimited identifier is always an identifier, never a key word.

    So if you want to use uppercase and any special character, you must (always) double quote the name:

    create table "TE#st" (id integer);
    select * from "TE#st";