2015-08-28 16 views
8

Ho un problema durante l'esecuzione di 1 progetto NodeJs con database PostgreSQL. Errore durante il tentativo di inserire dati in pgAdmin utilizzando il comando COPY.In PostgreSQL, come inserire i dati con il comando COPY?

COPY beer (name, tags, alcohol, brewery, id, brewery_id, image) FROM stdin; 

Bons Voeux blonde 9.5 Brasserie Dupont 250 130 generic.png 

Questi dati in gist:

Questo errore:

ERROR: syntax error at or near "Bons" 
SQL state: 42601 
Character: 1967 

I was create database like this and execute file .sql:

+0

Questo tipo di errore dipende dal tipo di framework di database che si sta utilizzando. Dovresti includere tali dettagli nella tua domanda. –

+0

@ vitaly-t, stavo aggiornando il metodo di file gif creare 1 database ed eseguire il file beers.sql ma ho un errore. –

+0

Per favore aiutatemi. –

risposta

12
COPY tbl FROM STDIN;

non è supportato da pgAdmin.
Si ottiene un errore di sintassi normale perché Postgres ottiene i dati come codice SQL.

Tre possibili soluzioni:

1. Usare un multi-fila INSERT invece:

INSERT INTO beer(name, tags, alcohol, brewery, id, brewery_id, image) 
VALUES 
    ('Bons Voeux', 'blonde', 9.5, 'Brasserie Dupont', 250, 130, 'generic.png') 
, ('Boerke Blond', 'blonde', 6.8, 'Brouwerij Angerik', 233, 287 'generic.png') 
; 

nota la differente (SQL) sintassi per valori come stringa o numerico letterali.

È possibile generare i dati con pg_dump using --inserts. Correlati:

2. Oppure chiamare lo script sulla riga di comando utilizzando psql. Come utente del sistema postgres:

psql beer -f beer.sql 

Assicurarsi che ci sia un indicatore di fine dei dati (\.) per il formato di default text. (Hai quello.) The documentation:

End of data can be represented by a single line containing just backslash-period (\.). An end-of-data marker is not necessary when reading from a file, since the end of file serves perfectly well; it is needed only when copying data to or from client applications using pre-3.0 client protocol.

3. o spostare i dati in un file separato (locale al server!), dire 'beer_data.csv' e utilizzare COPY .. FROM 'filename' nello script :

COPY beer (name, tags, alcohol, brewery, id, brewery_id, image) FROM '/path/to/beer_data.csv'; 

Quali opere in entrambi i modi.

+2

'pg_dump --inserts > dump.sql' ha fatto per me, grazie! –

1

Primo passo:

creare belgianbeers database su pgAdmin.

Secondo passo: aperta pronta e funzionante questa riga di comando:

psql -U Postgres -d belgianbeers -a -f birre.sql

Questa riga di comando esegue e aggiorna le tabelle del database.

-U = nome utente postgres

Problemi correlati