For example, you export the schema and data or only the schema of apple database of the user(role) john to backup.sql with psql which must be used to import non-archive files as shown below. *backup.sql is created if it doesn't exist and my answer explains how to export schema and data and my answer explains how to import archive backup.sql into orange database and the doc explains how to export and import databases:
pg_dump -U john apple > backup.sql
Or:
pg_dump -U john -s apple > backup.sql
Or, you export only the data of the specific tables of apple database of the user(role) john to backup.sql with only INSERT statement which has column names as shown below:
pg_dump -U john -a --column-inserts -t person -t animal apple > backup.sql
Then, you need to input a password after running the command above:
Password:
Now, you can import backup.sql into orange database of the user(role) john as shown below. *You have to create orange database before hand otherwise there is error and my answer explains how to create a database:
psql -U john -f backup.sql orange
Or:
psql -U john -d orange -f backup.sql
Or:
psql --username=john --file=backup.sql orange
Or:
psql --username john --file backup.sql orange
Or:
psql --username=john --dbname=orange --file=backup.sql
Or:
psql --username john --dbname orange --file backup.sql
Or, you can try these below which don't work on Windows:
psql -U john orange < backup.sql
psql -U john -d orange < backup.sql
psql --username=john --dbname=orange < backup.sql
psql --username john --dbname orange < backup.sql
Then, you need to input a password after running the command above:
Password for user john:
In addition, you can import backup.sql into orange database of the user(role) john without a password prompt by setting a password(e.g., banana) to PGPASSWORD as shown below:
PGPASSWORD=banana psql -U john -f backup.sql orange